Tkinter标签小部件中的下划线文字?

Zac*_*own 12 python windows label tkinter underline

我正在开发一个项目,要求我在Tkinter Label小部件中为某些文本加下划线.我知道可以使用下划线方法,但我似乎只能根据参数强调小部件的1个字符.即

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
Run Code Online (Sandbox Code Playgroud)

我需要能够强化小部件中的所有文本,我确信这是可能的,但是如何?

我在Windows 7上使用Python 2.6.

Bry*_*ley 15

要为标签小部件中的所有文本加下划线,您需要创建一个将下划线属性设置为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()
Run Code Online (Sandbox Code Playgroud)


dre*_*mon 8

对于那些使用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)
Run Code Online (Sandbox Code Playgroud)


Vig*_*esh 8

内线

mylabel = Label(frame, text = "my label", font="Verdana 15 underline")
Run Code Online (Sandbox Code Playgroud)


Ary*_*rke 6

试试这个下划线:

mylbl=Label(Win,text='my Label',font=('Arial',9,'bold','underline'))
mylbl.grid(column=0,row=1)
Run Code Online (Sandbox Code Playgroud)