How to Create a Calculator in Python Tkinter ActiveState How to Create a Calculator in Python Tkinter - (2024)

Tkinter is the most popular way to create User Interfaces in Python. This tutorial provides you with all the information you need to understand Tkinter and how to practically apply it.

How to Create a Calculator in Python Tkinter ActiveState How to Create a Calculator in Python Tkinter - (1)

Why Tkinter for Building Python GUIs?

Tkinter is popular because it’s a simple to use yet powerful tool. It also doesn’t hurt that Tkinter is the only UI framework built into the Python standard library. Tkinter is also a cross-platform tool, rendering GUIs using native operating system elements so that applications built with Tkinter look like they belong on whatever platform that they are installed on.

While Tkinter applications may look outdated compared to those built with more complex GUI frameworks, Tkinter is comparatively lightweight and painless to code with. For a more modern look, you can take advantage of the tkinter.ttk module (which provides access to the Tk themed widget set), which, along with the ttk.Style() class gives Tkinter applications a more modern look and feel.

When it comes to coding the application, Tkinter offers a simple, event-driven structure in which applications respond to user-driven events such as a mouse-click.

This tutorial is divided into easy to understand sections, each of which begins with a comment and are numbered for reference. As long as you have a recent version of Python installed, you can follow along with the code in each section:

  1. Import Tkinter
  2. Create a container
  3. Define widget functions
  4. Design the application
  5. Run the application

1) Import Tkinter

We’ll use Tkinter to build a calculator application that features basic arithmetic functions. Because recent versions of Python contain both Tkinter and basic math functions on the core library, all we need to do to get started is install Python.

For Windows users:

First, install the State Tool by running the following at a Powershell prompt:

IEX(New-Object Net.WebClient).downloadString('https://platform.activestate.com/dl/cli/install.ps1')

The State Tool will now automatically install Python 3.8 into a virtual environment for you when you run the following at a cmd prompt:

state activate --default ActiveState/ActivePython-3.8

For Linux or Mac users:

First, install the State Tool by running the following at a command prompt:

sh <(curl -q https://platform.activestate.com/dl/cli/install.sh)

The State Tool will now automatically install Python 3.8 into a virtual environment for you when you run the following:

state activate --default ActiveState/ActivePython-3.8

We can now go ahead and import everything from the Tkinter package:

from tkinter import *

2) Create a Tkinter Container

UI elements require a container/window in which they to place them. To create a root window, enter the following code, and provide a name (<any_name>) for it. You can also rename the window’s title from CALCULATOR to anything you wish.

<any_name> = Tk() root = Tk()

# Geometry method that defines the root window size: “width x height”:

root.geometry("312x324")

# Prevent root window from been resized:

root.resizable(0, 0)

# Window title:

root.title("CALCULATOR")

3) Define Tkinter Widget Functions

In this step, you’ll define what happens when a user clicks on a button in our calculator. We’ll need code that can:

  • Input a number or operation on btn_click()
  • Clear the input on btn_clear()
  • Calculate the input on btn_equal()

# 3a. btn_click function updates the input field whenever a number is entered. Any button pressed will act as a button click update:

def btn_click(item):
global expressionexpression = expression + str(item)input_text.set(expression)

# 3b. btn_clear function clears the input field and previous calculations using the ’Clear’ button:

def btn_clear():
global expressionexpression = ""input_text.set("")

# 3c. btn_equal function calculates the expression entered in the input field.

def btn_equal():global expression

# eval() function evaluates the string expressions directly:

 result = str(eval(expression)) input_text.set(result)expression = ""
expression = ""

# StringVar()’ is used to get the instance of the input field:

input_text = StringVar()

4) Design the Application

Now that we have the required code, we can:

  • Create a frame in which to place our input field
  • Create the input field, which will display the numbers and mathematical operations
  • Create a frame in which to place our buttons
  • Place the buttons in rows, and configure them to trigger the events defined in section 3

# 4a. Use the Frame widget to create a frame for the input field:

input_frame = Frame(root, width = 312, height = 50, bd = 0, highlightbackground = "black", highlightcolor = "black", highlightthickness = 1)input_frame.pack(side = TOP)

# 4b. Create an input field inside the Frame created in the previous step. Output aligned to the right:

input_field = Entry(input_frame, font = ('arial', 18, 'bold'), textvariable = input_text, width = 50, bg = "#eee", bd = 0, justify = RIGHT)input_field.grid(row = 0, column = 0)input_field.pack(ipady = 10)

# ‘ipady’ is internal padding that increases the height of the input field.

# 4c. Create a separate Frame below the input field, which will include buttons positioned in rows inside the frame:

btns_frame = Frame(root, width = 312, height = 272.5, bg = "grey")btns_frame.pack()

# The remainder of this section defines Button widgets arranged in 5 rows within a Frame widget. The buttons are positioned using the grid() Layout Manager. Each Button has an event that can be triggered with a mouse action configured in the previous section. E.g. btn_click()

# 4d. First row includes 2 buttons, ‘Clear (Clear)’ and ‘Divide (/)’:

clear = Button(btns_frame, text = "Clear", fg = "black", width = 32, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_clear()).grid(row = 0, column = 0, columnspan = 3, padx = 1, pady = 1)divide = Button(btns_frame, text = "/", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("/")).grid(row = 0, column = 3, padx = 1, pady = 1)

# 4e. Second row includes 4 buttons, ‘7’, ‘8’, ‘9’ and ‘Multiply (*)’:

seven = Button(btns_frame, text = "7", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(7)).grid(row = 1, column = 0, padx = 1, pady = 1)eight = Button(btns_frame, text = "8", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(8)).grid(row = 1, column = 1, padx = 1, pady = 1)nine = Button(btns_frame, text = "9", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(9)).grid(row = 1, column = 2, padx = 1, pady = 1)multiply = Button(btns_frame, text = "*", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("*")).grid(row = 1, column = 3, padx = 1, pady = 1)

# 4f. Third row includes 4 buttons, ‘4’, ‘5’, ‘6’ and ‘Subtract (-)’:

four = Button(btns_frame, text = "4", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(4)).grid(row = 2, column = 0, padx = 1, pady = 1)five = Button(btns_frame, text = "5", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(5)).grid(row = 2, column = 1, padx = 1, pady = 1)six = Button(btns_frame, text = "6", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(6)).grid(row = 2, column = 2, padx = 1, pady = 1)minus = Button(btns_frame, text = "-", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("-")).grid(row = 2, column = 3, padx = 1, pady = 1)

# 4g. Fourth row includes 4 buttons, ’1’, ’2’, ’3’, and ’plus (+)’:

one = Button(btns_frame, text = "1", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(1)).grid(row = 3, column = 0, padx = 1, pady = 1)two = Button(btns_frame, text = "2", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(2)).grid(row = 3, column = 1, padx = 1, pady = 1)three = Button(btns_frame, text = "3", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(3)).grid(row = 3, column = 2, padx = 1, pady = 1)plus = Button(btns_frame, text = "+", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("+")).grid(row = 3, column = 3, padx = 1, pady = 1)

# 4h. Fifth row includes 3 buttons, ‘0’, ‘Decimal (.)’, and ‘Equal To (=)’:

zero = Button(btns_frame, text = "0", fg = "black", width = 21, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(0)).grid(row = 4, column = 0, columnspan = 2, padx = 1, pady = 1)point = Button(btns_frame, text = ".", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click(".")).grid(row = 4, column = 2, padx = 1, pady = 1)equals = Button(btns_frame, text = "=", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_equal()).grid(row = 4, column = 3, padx = 1, pady = 1)

5) Run the Application

Now that we’ve placed all our GUI elements and tied them to the code needed to reflect user input, it’s time to try out our calculator by running the following command:

root.mainloop()How to Create a Calculator in Python Tkinter ActiveState How to Create a Calculator in Python Tkinter - (2)

Summary

This calculator tutorial exercises a number of common techniques that anyone working with Tkinter will need to become familiar with, including:

  • Coding events that define what happens when a user interacts with a UI element
  • Creating containers/windows that contain Frame widgets in which UI elements can be placed
  • Using Tkinter’s Layout Manager to position Button widgets within a Frame widget

There are three Layout Managers available for Tkinter: pack(), place(), grid().

  • pack() organizes widgets in horizontal and vertical boxes that are limited to left, right, top, bottom positions. Each box is offset and relative to each other.
  • place() places widgets in a two-dimensional grid using x and y absolute coordinates.
  • grid() arranges widgets in a two-dimensional grid using row and column absolute coordinates similar to a spreadsheet.

In addition, you can use padding (padx, pady)to modify the spacing between pack() sides, place() x,y coordinates, and grid()rows and columns. For example:

grid(row = 1, column = 2, padx = 1, pady = 1)

Button widget functions such as btn_click are positioned with grid(). For example:

command = lambda: btn_click(“/”)).grid(row = 0, column = 3, padx = 1, pady = 1)

The grid Layout Manager was chosen for the Calculator application because of its precision compared to other layout methods. After all, a calculator needs to be precise.

Get calculator.py source code here.

Next steps

Now that you know how to use Tkinter to create a calculator, let’s move on to other things you can do with Tkinter:

  • How To Use Pack In Tkinter
  • How To Install Tkinter In Windows
  • How To Position Buttons In Tkinter
  • What is Tkinter used for and how to install this Python Framework?

Get a version of Python that’s pre-compiled for Data Science

While the open source distribution of Python may be satisfactory for an individual, it doesn’t always meet the support, security, or platform requirements of large organizations.

This is why organizations choose ActivePython for their data science, big data processing and statistical analysis needs.

Pre-bundled with the most important packages Data Scientists need, ActivePython is pre-compiled so you and your team don’t have to waste time configuring the open source distribution. You can focus on what’s important–spending more time building algorithms and predictive models against your big data sources, and less time on system configuration.

Some Popular Python Packages for Data Science/Big Data/Machine LearningYou Get Pre-compiled – with ActivePython

  • pandas(data analysis)
  • NumPy(multi-dimensional arrays)
  • SciPy(algorithms to use with numpy)
  • HDF5(store & manipulate data)
  • Matplotlib(data visualization)
  • Jupyter(research collaboration)
  • PyTables(managing HDF5 datasets)
  • HDFS(C/C++ wrapper for Hadoop)
  • pymongo(MongoDB driver)
  • SQLAlchemy(Python SQL Toolkit)
How to Create a Calculator in Python Tkinter ActiveState How to Create a Calculator in Python Tkinter - (2024)

FAQs

Which is the best GUI calculator for Python? ›

Py-Calculator is a GUI calculator application with all the basic functionalities of a calculator written completely in Python3 using the tkinter framework.

What is a GUI calculator? ›

GUI calculator is only a calculator but one can use it by clicking buttons, not by typing something like in python print(2+2) , and then will get the result as 4 in the black and white environment. In the GUI calculator, all its operators and numbers are represented graphically and one can click it and use it.

What is a GUI calculator in Python? ›

GUI based simple calculator using Python Tkinter module, which can perform basic arithmatic operations addition, subtraction, multiplication and division.

What math can Python do that a calculator can t? ›

complex maths - although some calculators can do complex number operations, it isn't common; Python can do complex number operations out of the box. Fixed precision decimals - again not a common operation on calculators, Python can do fixed precision decimals by using the decimals.

How to create a calculator? ›

There are plenty of ways to do math on a desktop computer using a built-in calculator, but another way is to build one yourself using a simple HTML code. To create a calculator using HTML, learn some basics about HTML, then copy the necessary code into a text editor and save it with an HTML extension.

Is tkinter the best GUI for Python? ›

Simplicity vs. Power: If you are new to GUI programming or developing a small to medium-sized application, Tkinter is a good choice due to its simplicity and widespread adoption. If you need advanced features and customization options, PyQT or wxPython may be more suitable.

What is the fastest Python GUI? ›

Python Tkinter

It's a fast and easy-to-use Python GUI library, making it the go-to library for building a Python GUI application.

Does Python have a GUI? ›

Python has a lot of GUI frameworks, but Tkinter is the only framework that's built into the Python standard library. Tkinter has several strengths. It's cross-platform, so the same code works on Windows, macOS, and Linux.

How to import tkinter in Python? ›

Install the latest Python as a prerequisite using the link.
  1. Syntax. We'll use Python's import module syntax, as shown below. import module_name as modeule_alias_name.
  2. Import Tkinter. As shown below, we import module Tkinter with an alias name tk. import tkinter as tk.
  3. Example. Let's take a look at an example of this.

What is GUI method? ›

A graphical user interface (GUI) is a digital interface in which a user interacts with graphical components such as icons, buttons, and menus. In a GUI, the visuals displayed in the user interface convey information relevant to the user, as well as actions that they can take.

How to make a calculator in pyqt5? ›

GUI implementation steps
  1. Create a label to show the numbers and the output and set its geometry.
  2. Align the label text from right side and increase the font size of it.
  3. Create push buttons for the numbers from 0 to 9 and set their geometry in the proper order.
Jun 6, 2022

Can Python be used as a calculator? ›

Python can be used as a calculator to compute arithmetic operations like addition, subtraction, multiplication and division. Python can also be used for trigonometric calculations and statistical calculations.

How to make a calculator using Java Swing? ›

So, we used JFrame, JButton, and JPanel of java swing component to make the calculator's UI and functions like addActionListener() and getActionCommand() for adding actions that need to be performed when the buttons listen to any action on them. Finally, we got the following output by running the above code.

Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 6258

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.