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 is what it looks like:
As you can see,though I set width=60,height=40
,the width of text widget is smaller than its height.This keeps disturbing me every time I use tkinter.So my questions are:
What exactly does 40
and 60
mean?
What is the reason that text's width is smaller than its height?
What is the best way to do size controll?
An Entry widget in a Tkinter application supports singleline user inputs. You can configure the size of an Entry widget such as its width using the width property. However, tkinter has no height property to set the height of an Entry widget. To set the height, you can use the font('font_name', font-size) property.
In order to resize the font-size, font-family and font-style of Label widgets, we can use the inbuilt property of font('font-family font style', font-size).
when you specify the width and height they aren't in pixels; they are measured by characters and lines depending on the current font size
that's why when you do Text(self.tk,width=60,height=40)
the 60 means the text widget is 60 characters wide and the 40 means its 40 lines high
this also applies to Buttons
that's where the confusion comes from, because its not in pixels and if you change the font it ill change the size of the text widget!
and that's probably the best way to do size control its just confusing at first but now that you know it will make more sense!
take a look at this site Text Widget Info and look through the parameters for more information
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