Here's the code to create a tkinter window that appears at the center of the screen with a width of 250 pixels and height of 50 pixels:
main.py560 chars24 lines
In the above code, we first import the Tk
class from tkinter
. Then we create an instance of the Tk
class and store it in the root
variable.
After that, we set the width and height of the window to 250 and 50 pixels respectively.
We then get the width and height of the screen using winfo_screenwidth()
and winfo_screenheight()
methods of the root
window.
We calculate the x and y coordinates of the window such that it appears centered on the screen by subtracting the window width and height from the screen width and height, respectively.
Finally, we set the geometry of the window using the geometry()
method, passing it the window width, height, x-coordinate and y-coordinate as arguments.
We then start the event loop using the mainloop()
method. This creates the window and displays it at the center of the screen.
gistlibby LogSnag