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

发布于 2024-09-18 04:10:27 字数 347 浏览 12 评论 0原文

我正在开发一个项目,需要我在 Tkinter 标签小部件中的某些文本下划线。我知道可以使用下划线方法,但根据参数,我似乎只能让它为小部件的 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。

I am working on a project that requires me to underline some text in a Tkinter Label widget. I know that the underline method can be used, but I can only seem to get it to underline 1 character of the widget, based on the argument. i.e.

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

I need to be able to underline all the text in the widget, I'm sure this is possible, but how?

I am using Python 2.6 on Windows 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

梦开始←不甜 2024-09-25 04:10:27

要为标签小部件中的所有文本添加下划线,您需要创建一种新字体,并将下划线属性设置为 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()

To underline all the text in a label widget you'll need to create a new font that has the underline attribute set to True. Here's an example:

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()
抠脚大汉 2024-09-25 04:10:27

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

For those working on Python 3 and can't get the underline to work, here's example code to make it work.

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)
ζ澈沫 2024-09-25 04:10:27

内线

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

oneliner

mylabel = Label(frame, text = "my label", font="Verdana 15 underline")
ヅ她的身影、若隐若现 2024-09-25 04:10:27

试试这个下划线:

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

Try this for underline:

mylbl=Label(Win,text='my Label',font=('Arial',9,'bold','underline'))
mylbl.grid(column=0,row=1)
暗地喜欢 2024-09-25 04:10:27
p = Label(root, text=" Test Label", bg='blue', fg='white', font = 'helvetica 8 underline')

输入你自己的字体(我选择helvetica 8)

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

put your own font (i choose helvetica 8)

心凉怎暖 2024-09-25 04:10:27
mylabel = Label(frame, text = "my label")
mylabel.configure(font="Verdana 15 underline")
mylabel = Label(frame, text = "my label")
mylabel.configure(font="Verdana 15 underline")
蓝梦月影 2024-09-25 04:10:27

要为所有字符添加下划线,您应该导入 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()

To underline all the characters you should import tkinter.font and make your own font style with this. Example-

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()
疾风者 2024-09-25 04:10:27

应采用以下格式:

dev_label=Lab​​el(Right_frame, text="[email] protected]", font=("Times",15,"粗斜体下划线"), fg="black",bg="white")

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

should be in this format:

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文