Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set a widget's size in tkinter?

Tags:

python

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 is what it looks like:

enter image description here

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:

  1. What exactly does 40 and 60 mean?

  2. What is the reason that text's width is smaller than its height?

  3. What is the best way to do size controll?

like image 697
laike9m Avatar asked Jul 01 '13 06:07

laike9m


People also ask

How do you change the size of an entry in Tkinter?

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.

How do you change the size of a label in Python?

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


1 Answers

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

like image 132
Serial Avatar answered Oct 10 '22 04:10

Serial