In this tutorial, we will learn how to start the Tkinter window at a specific position of your display.
You can use the geometry method of the Tk class to set the window’s dimensions and position. The geometry method takes a string in the format “width x height + x +y”, which are the dimensions of the window, and x and y are the distances corner from the screen from the top-left corner of the screen to the top-left corner of the window.
Start the Tkinter window at a specific position on your display
Let’s first create a simple window.
import tkinter as tk newWindow = tk.Tk() newWindow.title("HELLO") newWindow.mainloop()
The output will be like this
Using the geometry() method,
For Example,
Here’s A Python Code demonstrating how to position a Tkinter window at a specific location :
import tkinter as tk root = tk.Tk() root.geometry("300x200+100+50") root.mainloop()
This will create a window with a width of 300 pixels, and a height of 200 pixels, and position the top-left corner of the window at coordinates (100,50) on your screen.
In this example:
- root = tk.Tk() creates the main window.
- root.mainloop() starts the Tkinter event loop, which waits for user interaction.
Procedure:
1. Import Tkinter: First, you need to import the Tkinter module.
2. Create the Main Window: Initialize a Tkinter window instance.
3. Set the Geometry: Use the geometry method to specify the size and position of the window.
4. Run the Application: Start the Tkinter event loop with mainloop().
You can adjust the width, height, x and y variables to position the window as needed on your display.
These examples show how to control a Tkinter window’s starting position and size for user interactions.