Python 3, Tkinter 8.6. GUI examples in Windows 10
When you create your first window in Tkinter, you can set it’s starting size and position on the screen by using the geometry
method.
When using this method, note that it only provides the window with the size and position when it is initialized. This means that the user can then change the size of the window and move it once it has first been put on the screen.
Initial Window Size
1 2 3 4 5 6 7 8 9 10 11 |
#Python 3, Tkinter 8.6 #Sizing the Main Window from tkinter import * root = Tk() root.geometry("500x100") #Width x Height root.mainloop() |
In line 8, geometry
is called to the root window. Inside the brackets, we first set the Width to 500 pixels and the Height to 100 pixels. Note that we are using a lowercase “x” here instead of a “*” to essentially say: I want the window to be 500 pixels by (x) 100 pixels.
Initial Window Position
When the first Tkinter window is run, it will usually appear in the top left-hand corner by default.
To change this we can add the height and width position to the geometry
method.
1 2 3 4 5 6 7 8 9 10 11 |
#Python 3, Tkinter 8.6 # Sizing and Positioning from tkinter import * root = Tk() root.geometry("500x100+300+300") root.mainloop() |
When Tkinter positions a window it references the top left corner of the window.
Here, we position the top left corner of the window right 300 pixels and down 300 pixels.
The geometry method in Line 8 now contains the following:
.geometry("window width x window height + position right + position down")
Note the “+” symbol before each position.
You can also just set the position of the window without adding the window width or height.
1 2 3 4 5 6 7 8 9 10 11 |
#Python 3, Tkinter 8.6 # Sizing and Positioning from tkinter import * root = Tk() root.geometry("+300+300") root.mainloop() |
Because we are not setting the width or height. Tkinter will automatically adjust its size to a 200 by 200-pixel window.
*”Window size geography.py” ? Yes, I have a dumb sense of humour.
Want to learn how to automate your daily admin at work with Python? Udemy has some great Python automation courses that will help you learn how to automate your tasks so you can focus on what really matters.
Got a more specific problem you need help with, but don’t have the time to develop the skills? Fiverr’s your best bet to find a skilled professional to solve your problem quickly and cheaply. *
*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.