I'm trying to create a text widget in Tkinter (for Python) whose font size can change, but when this happens I don't want the widget to resize itself. All of the text in the widget is the same font style. What I've got so far:
root = Tk()
t = Toplevel(root)
fnt = tkFont.Font(family="Helvetica",size=36,weight="bold",underline=1)
txt = Text(t, font=fnt, width=20, height=6)
txt.grid(row=0,column=0)
b = Button(t, text="click", command=change)
b.grid(row=1, column=0)
txt.insert(END, "This is text!")
Where change
is defined as:
def change():
txt.delete(1.0, END)
fnt.config(size=100)
txt.insert(END, "This is text!")
Now, when I click on the button, the text does indeed get bigger, but the entire widget resizes itself to compensate! I assume that this is because the size of the widget is specified in terms of "lines" and "characters", instead of pixels. How can I stop the widget from resizing?
I've tried not changing the font of the widget, but instead just inserting text with a tag that specifies a new font, which works, but then the problem is that when I manually type new text to the left and right of the inserted text, it is the default style and not the size I want.
I found the answer to my question on this page: How to stop Tkinter Text widget resize on font change?
The key is to build a Frame to put the text widget in, to specify the Frame size in pixels, and to call grid_propagate(False)
on the Frame to prevent it from resizing when the text widget wants to resize.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With