How to Create Window in Python Using Tkinter?

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.

  1. from tkinter import * # Import tkinter library in your Python Program.
  2. Window = Tk() # Declare a window object using Tk() method.
  3. 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

Tkinter create window example

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

See also:

Vinish Kapoor

An Oracle Apex Consultant, Oracle ACE, and founder of foxinfotech.in and orclqa.com a question and answer forum for developers.

This Post Has 12 Comments

  1. Mik

    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

    1. Daniel

      Do you know how to open multiple of these at once?

  2. Daniel

    Hello there. I would like to know how to open multiple of these at once. please let me know if you can help thanks

    1. Vinish Kapoor

      In the below example, it will open two windows (window and window1) at once:

      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')
      
      window1 = Tk()
      # set window title
      window1.title("Python GUI App")
      # set window width and height
      window1.configure(width=500, height=300)
      # set window background color
      window1.configure(bg='lightgray')
      window1.mainloop()
      
  3. Alpere

    How can I code, that each string in my array is in a separate line?

    1. Vinish Kapoor

      Try this:

      print(*yourlist, sep = "\n") 

  4. Aaqil

    hey I have two codes in one file please help on how to make a window with the code below in it

    from tkinter import *
    
    # declare the window
    window = Tk()
    # set window title
    window.title("Celsius To Farenhite")
    # set window width and height
    window.configure(width=500, height=500)
    # set window background color
    window.configure(bg='white')
    window.mainloop()
    
    def fahrenheit_from(celsius):
        """Convert Celsius to Fahrenheit degrees."""
        try:
            fahrenheit = float(celsius) * 9 / 5 + 32
            fahrenheit = round(fahrenheit, 3)  # Round to three decimal places
            return str(fahrenheit)
        except ValueError:
            return "invalid input"
    
    if __name__ == "__main__":
        celsius = input("Celsius: ")
        print("Fahrenheit:", fahrenheit_from(celsius))
    
  5. Iram

    Hi!
    How Can I Print In Some Text In The Window?

    1. Eggdog

      After you are done initalizing the window, put this code before root.mainloop()

      p_one = Text(root, height=0)
      p_one.pack()
      

      You should also make sure to change root to your window name

    2. mauuuuu

      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?

  6. USR287

    It is NOT working on VS code for ubuntu linux if you can fix this i would be delighted

  7. fazee

    how to create a window inside a parent window?

Comments are closed.