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

问题描述 投票:0回答:8

我正在做一个项目,需要我在 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

我需要能够在小部件中的所有文本下划线,我相信这是可能的,但是怎么做呢?

我在 Windows 7 上使用 Python 2.6。

python windows label tkinter underline
8个回答
24
投票

要为标签小部件中的所有文本添加下划线,您需要创建一种新字体,将下划线属性设置为 True。这是一个例子:

try:
    import Tkinter as tk
    import tkFont
except ModuleNotFoundError:  # Python 3
    import tkinter as tk
    import tkinter.font as 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()

15
投票

对于那些在 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)

8
投票

oneliner

mylabel = Label(frame, text = "my label", font="Verdana 15 underline")

5
投票

试试这个下划线:

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

2
投票
mylabel = Label(frame, text = "my label")
mylabel.configure(font="Verdana 15 underline")

2
投票
p = Label(root, text=" Test Label", bg='blue', fg='white', font = 'helvetica 8 underline')

放自己的字体(我选择helvetica 8)


1
投票

要在所有字符下划线,您应该导入 tkinter.font 并使用它制作您自己的字体样式。示例-

from tkinter import *
from tkinter.font import Font
rt=Tk()
myfont=Font(family="Times",size=20,weight="bold", underline=1)
Label(rt,text="it is my GUI".title(),font=myfont,fg="green").pack()
rt.mainloop()

0
投票

应该是这样的格式:

dev_label=Label(Right_frame, text="[email protected]", font=("Times",15,"bold italic underline"), fg="black",bg="white")

dev_label.place(x=80,y=120)

© www.soinside.com 2019 - 2024. All rights reserved.