In this tutorial, you will learn how to create a window in Python using the Tkinter module library.
Create a Window in Python Using Tkinter Example
The essential steps to creating a window using Tkinter.
- from tkinter import * # Import tkinter library in your Python Program.
- Window = Tk() # Declare a window object using Tk() method.
- Window.mainloop() # End the program using the mainloop() method for the window. This method holds the window active.
Complete Python Program
from tkinter import * # declare the window window = Tk() # set window title window.title("Python GUI App") # set window width and height window.configure(width=500, height=300) # set window background color window.configure(bg='lightgray') window.mainloop()
Output
Moving a Window in Center
The following highlighted code will open the window in the center using geometry() method.
from tkinter import * window = Tk() window.title("Python GUI App") window.configure(width=500, height=300) window.configure(bg='lightgray') # move window center winWidth = window.winfo_reqwidth() winwHeight = window.winfo_reqheight() posRight = int(window.winfo_screenwidth() / 2 - winWidth / 2) posDown = int(window.winfo_screenheight() / 2 - winwHeight / 2) window.geometry("+{}+{}".format(posRight, posDown)) window.mainloop()
Hello Vinish,
I'm looking for a way to use Tkinter to display a second fullscreen window on my MacBook's external monitor, but I can't find a solution anywhere....
On the internal monitor it's not a problem.
Greetings, Mik
Do you know how to open multiple of these at once?
Hello there. I would like to know how to open multiple of these at once. please let me know if you can help thanks
In the below example, it will open two windows (window and window1) at once:
How can I code, that each string in my array is in a separate line?
Try this:
print(*yourlist, sep = "\n")
hey I have two codes in one file please help on how to make a window with the code below in it
Hi!
How Can I Print In Some Text In The Window?
After you are done initalizing the window, put this code before root.mainloop()
You should also make sure to change root to your window name
But with this function the whole height and width of the window are readjusted to the pack() values. Is it possible to make this a small widget in the original main window?
It is NOT working on VS code for ubuntu linux if you can fix this i would be delighted
how to create a window inside a parent window?