Learnmonkey Learnmonkey Logo
× Warning:

The tutorial you are reading is currently a work in-progress.

Your First Tkinter Program

Writing the Code

Below is the code:


import tkinter as tk
window = tk.Tk()
window.mainloop()
    

Running the Code

Run the code as you would run any other Python program. After running it, you should see a totally empty window like this:

An empty Tkinter window

As you can see, Tkinter windows look different depending on the operating system.

Code Breakdown

import tkinter as tk tells Python to import Tkinter. We don't want to type the seven characters tkinter whenever we want to do something, so we import Tkinter as if its name was tk.

window = tk.Tk() tells Tkinter to make a window object, which represents the main window of the program.

window.mainloop() tells Tkinter to run a loop so that whenever the user does something, like click a button, Tkinter can respond.