GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python GUI
  • AVINASH NETHALA
  • 4 years ago4 years ago
  • Next: Python program to print Fibonacci sequence

This view

  • Original page
  • New address

GeezerVison is showing www.programminginpython.com — A simple python GUI calculator using TKInter - Programming In Python

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 the basic introduction to GUI programming in Python, here I will use those and also extend some other things to build this simple app. So get ready for a simple python GUI calculator using TKInter.

You can also watch the video on YouTube here

Program on Github

Below are the screenshots, how what our basic app looks like,

A simple Python GUI calculator using TKInter

A simple Python GUI calculator using TKInter

 

Here I used, a 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 them.

Firstly import and initialize TKInter,

import tkinter as tk
..
..
root = tk.Tk()

I also set the size of the app and its title,

root.geometry('400x200+100+200')
root.title('Simple Calculator')

I also added some labels and input fields to read the entered numbers,

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 the above.

Program on Github

So when the button is clicked, a function is called which calculates the sum of both the numbers.

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.

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

Program on GitHub

Feel free to look at my previous posts on Python GUI programming.

Program

__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.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • More
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on WhatsApp (Opens in new window) WhatsApp

Online Python Compiler

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • More
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on WhatsApp (Opens in new window) WhatsApp

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

Layout Managers in Python GUI – Pack, Grid and Place

Layout Managers in Python GUI – Pack, Grid and Place

AVINASH NETHALA4 years ago4 years ago 0
Introduction to Python GUI – Labels, Frames, Buttons

Introduction to Python GUI – Labels, Frames, Buttons

AVINASH NETHALA4 years ago4 years ago 0
How to create a Domain to IP converter in Python GUI using TKInter

How to create a Domain to IP converter in Python GUI using TKInter

AVINASH NETHALA5 years ago4 years ago 0
How to create a temperature converter app in python GUI using TKInter

How to create a temperature converter app in python GUI using TKInter

AVINASH NETHALA6 years ago4 years ago 0

Recent Posts

  • Python vs R: Which Language is Better for Data Science?
  • Top 7 Python Books You Must Read in 2025
  • Best Python Web Frameworks: Django, Flask, and FastAPI Breakdown
  • 20 Best Python One-Liners to Boost Your Coding Skills
  • Matplotlib Data Visualization in Python

Suggested Courses

  • Complete Python Developer: Zero to Mastery.
  • Automate the boring stuff.
  • Python and Django Full Stack Web Developer Bootcamp.
  • Artificial Intelligence and Machine Learning E-Degree.
  • Complete Python & Python OOP.

Subscribe to our newsletter!

Great Deals

Ads
  • Health and Beauty
  • Deals on Electronics
  • Toys and Games
  • Sports and Outdoors
  • Pet Supplies
  • Shoes
  • Movies and TV

Categories

  • Algorithms (8)
  • Artificial Intelligence AI (3)
  • Basic Programs (25)
  • Books (1)
  • Data Structures (10)
  • Databases (1)
  • Dictionaries (1)
  • Files Handling (1)
  • Letter Patterns (26)
  • Lists (7)
  • Machine Learning (3)
  • Math Programs (6)
  • Pattern Programming (31)
  • Promoted (1)
  • Python for Automation (1)
  • Python for Data Science (6)
  • Python for Ethical Hacking (2)
  • Python For Finance (4)
  • Python GUI (5)
  • Python Libraries (6)
  • Python OOP (1)
  • Python Resources (3)
  • Python Tips and Tricks (10)
  • Searching Algorithms (2)
  • Sets (1)
  • Sorting Algorithms (6)
  • Tuples (1)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.