Setting the size of a text widget Tkinter in pixels?

I am trying to create a text widget in Tkinter (for Python), the font size of which can change, but when this happens, I do not want the widget to resize. All text in widgets is the same font style. What I have 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 press the button, the text really gets bigger, but the whole widget is resized to compensate! I assume that this is because the widget size is specified in terms of โ€œstringsโ€ and โ€œcharactersโ€ instead of pixels. How can I stop widget resizing?

I tried not to change the font of the widget, but instead just inserted text with a tag that indicates a new font that works, but then the problem is that when I manually type new text to the left and right of the inserted text, this is the default style, and not the size i want.

+4
source share
1 answer

I found the answer to my question on this page: How to stop the resizing of the Tkinter Text widget when changing the font?

The key is to create a frame for entering the text widget, specify the frame size in pixels, and call grid_propagate(False) on the frame so that it does not change when the text widget wants to resize.

+5
source

Source: https://habr.com/ru/post/1410885/


All Articles