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

发布于 2021-01-29 14:58:36

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

关注者
1
被浏览
107
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看