Tkinter Window

In this tutorial, we will learn how to create a simple Tkinter window in python.

Tkinter is the built-in library that is most commonly used to develop GUI applications using python. This library is a python wrapper around the original TCL GUI toolkit. This library is easy to understand and suitable for beginners.

Other popular options to create GUI apps in python are wxpython, PyQt5, kivy, etc.

1. Create Window

In this code example, we will create a simple GUI window.

from tkinter import *

# Create a window
window = Tk()

# Place widgets here!

# run window mainloop
window.mainloop()

Code Explaination :

  • We first imported everything from the Tkinter library.
  • We then created the main window by calling the Tk() class of the Tkinter library.
  • We then call the mainloop() on our window. It’s an infinite loop that must be called for the window to be drawn and events to be processed.

Save and run the above code.you should see the following output.

Output :

Tkinter window code example output

2. Tkinter Window Title and Dimensions

In this code example. we will set the title and custom dimensions of our window by calling the .title() and .geometry() methods on the window object.

from tkinter import *

window = Tk()

window.title("My title") # Set the title of the window
window.geometry("300x300") # Set the height and width of the window

window.mainloop()

Here is the output of the above code.

change title and geometry of tkinter window

As you can see, title and size of the window has been changed.

3. Change Window Icon

In the below code example, we will set our own icon in the tkinter window.

from tkinter import *

window = Tk()

p1 = PhotoImage(file = 'dog.png') # Load the image

window.iconphoto(False, p1) # Set the icon

window.mainloop()

Output :

tkinter window icon changed

4. Change Window Color

In this code example, we will change the background color of the window to black.

from tkinter import *

window = Tk()

window.configure(bg='black') # Set the background color to black.

window.mainloop()

Output :

change background color of the window.

Note : instead of giving the color name you can also give hexadecimal value of any color.

5. Mini Gui Project

In this final code example, we will use the Label, Entry, and Button widgets provided by Tkinter to create a mini-project. where a user can enter any number in the entry widget and there will be a button which on mouse click will find the square of that number and display on the window.

from tkinter import *

window = Tk() # Create window
window.title("Find Square") # Set title

# Create Label
Label(window,text = "Enter Number : ").grid(row =0,column = 0)

e = Entry(window) # Create entry
e.grid(row =0,column = 1)

def btn_click():
    # Function to be called on button click
    try:
        n = int(e.get()) # Get the entry text
        sq = str(n**2) # Find square and convert it to string
        result.set(f"Square of {n} : {sq}")
    except:
        print("Invalid Input")

Button(window,command = btn_click,text = "Find Square").grid(row = 0,column = 2)

result = StringVar() # String var to store result
Label(window,textvariable = result,bg = "red",fg = "white").grid(row = 1,column = 1)

window.mainloop()

Output :

mini project code output

That’s wrap for this tutorial.

Leave a Reply