【问题标题】:Underline Text in Tkinter Label widget?Tkinter Label小部件中的下划线文本?
【发布时间】:2011-04-08 23:59:58
【问题描述】:

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

【问题讨论】:

    标签: python windows label tkinter underline


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:
        mylabel = Label(frame, text = "my label")
        mylabel.configure(font="Verdana 15 underline")
        

        【讨论】:

          【解决方案4】:

          单线

          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)
            

            【讨论】:

              【解决方案6】:

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

              【讨论】:

                【解决方案7】:
                p = Label(root, text=" Test Label", bg='blue', fg='white', font = 'helvetica 8 underline')
                

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

                【讨论】:

                  猜你喜欢
                  • 2010-12-25
                  • 1970-01-01
                  • 2016-03-13
                  • 2021-04-02
                  • 2016-02-16
                  • 2017-09-03
                  • 1970-01-01
                  • 2020-08-18
                  • 2015-09-27
                  相关资源
                  最近更新 更多