2010-09-07 136 views
7

我正在研究一个项目,需要我在Tkinter Label小部件中强调一些文本。我知道可以使用下划线方法,但我似乎只能根据参数来强调小部件的1个字符。即Tkinter Label小部件中的下划线文本?

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 

我需要能够强调小部件中的所有文本,我相信这是可能的,但怎么做?

我使用Python 2.6在Windows 7

回答

12

为了强调在一个标签窗口小部件,您需要创建一个具有下划线属性设置为True一个新的字体中的所有文本。举个例子:

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

完美!谢谢! – 2010-09-07 17:24:28

0

对于那些使用Python 3并且无法获得下划线的人来说,下面是使它工作的示例代码。

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)