How to set widget size in tkinter?

simple example:

from tkinter import * class GUI_CMP(): def __init__(self): self.tk = Tk() self.text = Text(self.tk,width=60,height=40) self.text.pack() self.tk.mainloop() if __name__ == '__main__': gui_cmp = GUI_CMP() 

Here's what it looks like:

enter image description here

As you can see, although I set width=60,height=40 , the width of the text widget is less than its height. This bothers me every time I use tkinter.So my questions are:

  • What does 40 and 60 mean?

  • What is the reason that the width of the text is less than its height?

  • What is the best way to manage size?

+7
source share
1 answer

when you specify the width and height, they are not in pixels; they are measured in characters and strings depending on the current font size

that's why when you do Text(self.tk,width=60,height=40) 60 means the text widget is 60 characters wide, and 40 means its height is 40 lines.

this also applies to buttons

this is where the confusion comes from, because it's not in pixels, and if you change the font, it will resize the text widget!

and this is perhaps the best way to control size, at first itโ€™s just confusing, but now that you know it will make more sense!

take a look at this Text Widget Info website and view options for more information.

+8
source

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


All Articles