Underline Text in Tkinter Label widget? Underline Text in Tkinter Label widget? tkinter tkinter

Underline Text in Tkinter Label widget?


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 tkFontexcept ModuleNotFoundError:  # Python 3    import tkinter as tk    import tkinter.font as tkFontclass 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()


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 framepref = Label(checkFrame, text = "Select Preferences")# Pack or use grid to place the framepref.grid(row = 0, sticky = W)# font.Font instead of tkFont.Fonf = font.Font(pref, pref.cget("font"))f.configure(underline=True)pref.configure(font=f)


oneliner

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