Hello everybody, Welcome back to programminginpython.com! Now am going to show you how to make a simple python GUI application, basically, the app is a simple calculator which adds two numbers. I used TKInter which is a standard GUI package for python. In the previous post I covered about the basic introduction to GUI programming in python, here I will use those and also extend some other things to build this simple app.
You can also watch the video on YouTube here
Below are the screenshots, how our basic app looks like,


Here I used, grid layout for designing the layout, in the previous post, I told about pack() which sets the widgets automatically based on available space in the window, but here we can set the widgets where we want to place.
Firstly import and initialize TKInter,
|
1 2 3 4 |
import tkinter as tk .. .. root = tk.Tk() |
I also set the size of the app and its title,
|
1 2 |
root.geometry('400x200+100+200') root.title('Simple Calculator') |
I also added some labels and input fields to read the entered numbers,
|
1 2 3 4 5 6 7 8 |
labelTitle = tk.Label(root, text="Simple Calculator").grid(row=0, column=2) labelNum1 = tk.Label(root, text="Enter a number").grid(row=1, column=0) labelNum2 = tk.Label(root, text="Enter another number").grid(row=2, column=0) labelResult = tk.Label(root) labelResult.grid(row=7, column=2) entryNum1 = tk.Entry(root,textvariable=number1).grid(row=1, column=2) entryNum2 = tk.Entry(root,textvariable=number2).grid(row=2, column=2) |
Then I add a button, when clicked, calculates the result and displays it in the label labelResult I initialized above.
So when the button is clicked, a function is called which calculates the sum of both the numbers.
|
1 2 |
call_result = partial(call_result, labelResult, number1, number2) buttonCal = tk.Button(root, text="Calculate", command=call_result).grid(row=3, column=0) |
The command parameter to Button function calls the call_resultfunction, which does the simple logic.
The call_result function.
|
1 2 3 4 5 6 |
def call_result(label_result, n1, n2): num1 = (n1.get()) num2 = (n2.get()) result = int(num1)+int(num2) label_result.config(text="Result is %d" % result) return |
Feel free to look at my previous posts on python GUI programming.
Program
|
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 |
__author__ = 'Avinash' import tkinter as tk from functools import partial def call_result(label_result, n1, n2): num1 = (n1.get()) num2 = (n2.get()) result = int(num1)+int(num2) label_result.config(text="Result is %d" % result) return root = tk.Tk() root.geometry('400x200+100+200') root.title('Simple Calculator') number1 = tk.StringVar() number2 = tk.StringVar() labelTitle = tk.Label(root, text="Simple Calculator").grid(row=0, column=2) labelNum1 = tk.Label(root, text="Enter a number").grid(row=1, column=0) labelNum2 = tk.Label(root, text="Enter another number").grid(row=2, column=0) labelResult = tk.Label(root) labelResult.grid(row=7, column=2) entryNum1 = tk.Entry(root, textvariable=number1).grid(row=1, column=2) entryNum2 = tk.Entry(root, textvariable=number2).grid(row=2, column=2) call_result = partial(call_result, labelResult, number1, number2) buttonCal = tk.Button(root, text="Calculate", command=call_result).grid(row=3, column=0) root.mainloop() |
Thatβs it for this post guys, also feel free to look at other python tutorials on GUI Programming.
Pingback: educational websites for students
Pingback: school education
Pingback: online education programs