2010-09-07 167 views
7

Estoy trabajando en un proyecto que me obliga a subrayar algunos textos en un widget Tkinter Label. Sé que se puede usar el método de subrayado, pero solo puedo hacer que subraye el carácter 1 del widget, basado en el argumento. es decir,¿Texto subrayado en el widget Etiqueta Tkinter?

p = Label(root, text=" Test Label", bg='blue', fg='white', underline=0) 

change underline to 0, and it underlines the first character, 1 the second etc 

que necesito para poder hacer hincapié en todo el texto en el widget, estoy seguro de que esto es posible, pero ¿cómo?

estoy usando Python 2.6 en Windows 7.

Respuesta

12

Para subrayar todo el texto en un control de etiqueta que tendrá que crear un nuevo tipo de letra que tiene el atributo de subrayado se establece en True. He aquí un ejemplo:

import Tkinter as tk 
import tkFont 

class App: 
    def __init__(self): 
     self.root = tk.Tk() 
     self.count = 0 
     l = tk.Label(text="Hello, world") 
     l.pack() 
     # clone the font, set the underline attribute, 
     # and assign it to our widget 
     f = tkFont.Font(l, l.cget("font")) 
     f.configure(underline = True) 
     l.configure(font=f) 
     self.root.mainloop() 


if __name__ == "__main__": 
    app=App() 
+0

Perfecto! ¡Gracias! –

0

Para aquellos que trabajan en Python 3 y no puede conseguir el subrayado al trabajo, aquí está el código ejemplo para hacer que funcione.

from tkinter import font 

# Create the text within a frame 
pref = Label(checkFrame, text = "Select Preferences") 
# Pack or use grid to place the frame 
pref.grid(row = 0, sticky = W) 
# font.Font instead of tkFont.Fon 
f = font.Font(pref, pref.cget("font")) 
f.configure(underline=True) 
pref.configure(font=f) 
Cuestiones relacionadas