In this Python TKinter article i want to talk about Building Interactive GUI Applications with Python Tkinter, so Python is powerful programming language and when it comes to building GUI Application, you have different libraries to use, one of them is Python TKinter.
What is GUI ?
Graphical User Interfaces (GUIs) are the standard way of interacting with modern computer applications. GUI allow users to interact with software applications in more simple and user friendly approach. one of the most popular frameworks for building GUI applications in Python is Tkinter. In this article we are going to learn how to use Tkinter to create interactive GUI applications.
What is Python TKinter ?
Tkinter is standard Python library for creating graphical user interfaces (GUIs). TKinter provides different tools and widgets for building windows, dialogs, menus and other GUI components.
name Tkinter comes from Tk interface, which refers to the fact that Tkinter is Python interface to the Tk GUI toolkit. Tk is cross platform GUI toolkit that provides different widgets and tools for building desktop applications.
Now let’s start building our GUI Application with Python TKinter, so Tkinter is builtin Python library and you don’t need to install it separately. you can import it into your Python code by using following command:
1 |
import tkinter as tk |
So now for creating basic Python GUI Window in Tkinter, you can use Tk() method to initialize new window object. after that you can use title() method to set the title of window. and lastly you can use the mainloop() method to start the event loop that listens for user actions.
1 2 3 4 5 6 |
import tkinter as tk root = tk.Tk() root.title("My Window") root.mainloop() |
If you run this code, you will see nice and simple GUI window that is built in Python TKinter.
So it was just basic GUI Window in TKinter, now let’s add some widgets to our GUI window, as we have already mentioned that TKinter provides different types of widgets that you can use within your application. for adding widget to Tkinter window, we can create an instance of the widget and add it to the window using pack() method. this is an example of how to add a button to Tkinter window:
1 2 3 4 5 6 7 8 9 |
import tkinter as tk root = tk.Tk() root.title("GeeksCoders") button = tk.Button(root, text="Click me!") button.pack() root.mainloop() |
Run your code and you will see button in your window.
Now let’s talk about event handling in Python TKinter, to make our GUI application interactive, we need to handle events, for example button clicks or text changes. Tkinter provides a way to bind event handlers to widgets using bind() method. this is the example for event handling in Python TKinter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import tkinter as tk def button_click(): print("Button clicked!") root = tk.Tk() root.title("GeeksCoders") button = tk.Button(root, text="Click me!") button.pack() button.bind("<Button-1>", lambda event: button_click()) root.mainloop() |
In the above code we have define a function button_click() that prints a message when button is clicked. after that we bind this function to the button using bind() method.
It is time to create complex example with Python TKinter, for creating more complex GUI applications, we can combine multiple widgets and event handlers to create more interactive user interface. this is an example of how to create simple calculator application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import tkinter as tk def button_click(number): current = e.get() e.delete(0, tk.END) e.insert(0, str(current) + str(number)) def button_clear(): e.delete(0, tk.END) def button_add(): first_number = e.get() global f_num global math math = "addition" f_num = int(first_number) e.delete(0, tk.END) def button_equal(): second_number = e.get() e.delete(0, tk.END) if math == "addition": e.insert(0, f_num + int(second_number)) root = tk.Tk() root.title("Calculator") e = tk.Entry(root, width=35, borderwidth=5) e.grid(row=0, column=0, columnspan=3, padx=10, pady=10) button_1 = tk.Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1)) button_2 = tk.Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2)) button_3 = tk.Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3)) button_4 = tk.Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4)) button_5 = tk.Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5)) button_6 = tk.Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6)) button_7 = tk.Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7)) button_8 = tk.Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8)) button_9 = tk.Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9)) button_0 = tk.Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0)) button_add = tk.Button(root, text="+", padx=39, pady=20, command=button_add) button_equal = tk.Button(root, text="=", padx=91, pady=20, command=button_equal) button_clear = tk.Button(root, text="Clear", padx=79, pady=20, command=button_clear) button_1.grid(row=1, column=0) button_2.grid(row=1, column=1) button_3.grid(row=1, column=2) button_4.grid(row=2, column=0) button_5.grid(row=2, column=1) button_6.grid(row=2, column=2) button_7.grid(row=3, column=0) button_8.grid(row=3, column=1) button_9.grid(row=3, column=2) button_0.grid(row=4, column=0) button_clear.grid(row=4, column=1, columnspan=2) button_add.grid(row=5, column=0) button_equal.grid(row=5, column=1, columnspan=2) root.mainloop() |
Above code defines four functions for handling button clicks, adding numbers, clearing display and calculating sum. after that it creates Tkinter window with an entry box for displaying the numbers and buttons for entering the digits, adding them.
Run your code and you will see nice Python TKinter GUI Window Calculator