Underline Text in Tkinter Label widget? - python

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.

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()

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)

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

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

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

p = Label(root, text=" Test Label", bg='blue', fg='white', font = 'helvetica 8 underline')
put your own font (i choose helvetica 8)

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()

should in this format:
dev_label=Label(Right_frame,text="purxxx#gmail.com", font=("Times",15,"bold italic underline"),fg="black",bg="white")
dev_label.place(x=80,y=120)

Related

Treeview moves after click on Combobox with Tkinter

I have developped a app in Python (tested with 3.8 and 3.9 on Windows 10) with Tkinter. I am using a Combobox and a Treeview. I want to change dynamically the width of dropdown listbox and I could do it by changing the style of TCombobox with the parameter postoffset.
However, everytime I click on the Combobox, the table next to it moves and extends its width by itself. I really don't know where this problem comes from.
I have created a simple code so that you can reproduce the problem.
import tkinter as tk
from tkinter import ttk
import tkinter.font as tkfont
def combo_configure(event, style):
combo = event.widget
long = max(combo.cget('values'), key=len)
font = tkfont.Font(family="Helvetica", size=10)
width = max(0,font.measure(long.strip() + '0') - combo.winfo_width())
style.configure('TCombobox', postoffset=(0,0,width,0))
win = tk.Tk()
win.geometry("850x250")
style = ttk.Style()
f = tk.Frame(win)
f.configure(bg="black")
f.pack()
combo = ttk.Combobox(f, style="TCombobox", values=["It's a test on Combobox style."], width=10)
combo.bind('<ButtonPress>', lambda e : combo_configure(e, style))
combo.pack(side="left")
tree = ttk.Treeview(f, column=["Column 1"], show='headings', height=10)
tree.heading(0, text="Column 1")
tree.column(0, anchor=tk.CENTER, width=125)
tree.pack()
win.mainloop()
Thanks.

Positioning text inside OptionMenu in tkinter python

hy I am working on tkinter python project.
I am trying to position text to the Left inside OptionMenu using anchor option but it does not seem to work. I am using ttk theme widgets.
Here is the code that I am trying currently.
s = ttk.Style()
s.configure('my.TMenubutton', font=("Cambria", fontSize, "bold"), background="white", anchor = W )
shapeMenu = ttk.OptionMenu(shapeFrame, shape, myShapes[1], *myShapes, style='my.TMenubutton', command=getShapes)
What I am doing wrong ?
It seems like there is no anchor option under TMenubutton. I tried TLabel, and it worked. Don't know if there is any side effect though.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('300x100')
CHART_TYPES = ('Reticle', 'Circle Grid', 'Checkerboard', 'Grille', 'Slanted Edge MTF')
s = ttk.Style()
s.configure('my.TLabel', font=("Cambria", 10, "bold"), background="white", anchor = 'w' )
chart_type = tk.StringVar()
chart_type.set(CHART_TYPES[0])
# chart_selector = tk.OptionMenu(root, chart_type, *CHART_TYPES)
chart_selector = ttk.OptionMenu(root, chart_type, *CHART_TYPES, style='my.TLabel')
# chart_selector.configure(anchor='w')
chart_selector.pack(expand=True, fill='x')
root.mainloop()

How can I do things like bold/highlight/change the font on only the text that I select?

I'm making a simple word processor and I want to be able to change the font/style (or whatever it's called) of only the text that I highlight.
I can't say what I've tried because I don't even know where to start.
from tkinter import *
# window setup
tk = Tk()
# main textbox
textbox = Text(tk)
textbox.configure(width=85,height=37)
textbox.grid(column=0,row=0,rowspan=500)
# bold
def bold():
textbox.config(font=('Arial',10,'bold'))
bBut = Button(tk,text='B',command=bold)
bBut.configure(width=5,height=1)
bBut.grid(column=0,row=0)
tk.mainloop()
I can change the entire text to bold/italic/etc. but I want to be able to specify parts of it.
It use tags to assign color or font to text
import tkinter as tk
def set_bold():
try:
textbox.tag_add('bold', 'sel.first', 'sel.last')
except Exception as ex:
# text not selected
print(ex)
def set_red():
try:
textbox.tag_add('red', 'sel.first', 'sel.last')
except Exception as ex:
# text not selected
print(ex)
root = tk.Tk()
textbox = tk.Text(root)
textbox.pack()
textbox.tag_config('bold', font=('Arial', 10, 'bold'))
textbox.tag_config('red', foreground='red')
button1 = tk.Button(root, text='Bold', command=set_bold)
button1.pack()
button2 = tk.Button(root, text='Red', command=set_red)
button2.pack()
root.mainloop()

Python Tkinter Text Alignment

I am using Tkinter with Python2 to create a new window by clicking on the button. In this new window I want to Display text. Now I have Troubles with the alignment, is it possible to align it to the left? It is always centered and neither anchor=LEFT nor sticky="NSEW" help.
import tkinter as tki
btn3 = tki.Button(self.root, text="HELP", command=self.help, fg="black", bg="white", font=("Courier",22))
btn3.grid(row=1, column=2, padx=10, pady=10, sticky="NSEW" )
def help(self):
self.text = """ Hello.
You can find help here to the following subjects:
- getting started
- installation
- FAQ."""
self.top = tki.Toplevel()
self.top.title("Help")
self.label1 = tki.Label(self.top, text = self.text, height = 0, width=80, fg="black", bg="white", font=("Courier",18))
self.label1.pack()
When you use anchor = n you need to put anchor = "n" i.e. with the n in quotes. You're getting the global name is not defined error because of this.
Alternatively, use anchor = tki.N. Because you used import tkinter as tki you must prefix tkinter variables with tki. in order for python to know what you're referring to. However, I think you might want to try justify=tki.LEFT also if that doesn't work.

Italicize tkinter label text on focus

I have a tkinter label
back_button = Label(self.about_frame, text = "Back", bg="black", fg="white", font=("Silkscreen", 18))
and I want to have the text's font change from regular to italic when a user hovers over the label with their mouse. How should I go about implementing this? Thanks!
import tkinter
from functools import partial
def font_config(widget, fontslant, event):
widget.configure(font=fontslant)
parent = tkinter.Tk()
text = tkinter.Label(parent, text="Hello Text")
text.bind("<Enter>", partial(font_config, text, "Helvetica 9 italic"))
text.bind("<Leave>", partial(font_config, text, "Helvetica 9"))
text.pack()
tkinter.mainloop()
See: this and this for more information.

Categories