Arithmetic Operations in Python using tkinter GUI
Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.
Note: tkinter vs Tkinter
Some features of Tkinter do not exist in tkinter and vice versa.The main difference is that Tkinter is a module in Python 2x while tkinter is a module in Python 3x.
- The foundational element of a Tkinter GUI is the window. Windows are the containers in which all other GUI elements live. These other GUI elements, such as text boxes, labels, and buttons, are known as widgets. Widgets are contained inside of windows.
- tkinter framework is cross platform so that same code works on windows, mac and linux operating system.
- Import tkinter
import tkinter
Or, more often:
from tkinter import *
- Set up application object by calling TK( ) function.
- This will create a window with title bar, control box with minimize, Maximize and close buttons and a client are to store the widgets
- The geometry( ) method is used to define height, width and coordinates of the frame.
- The application object enters into event listening loop by calling mainloop().
- Buttons are created using button class.
Output:
- Entry widget is a single line text box that accepts data from the user.
Ouput:
- Label can be created using label class
Ouput:
- place() method in Tkinter
The place geometry manager organizes widgets by placing them in a specific position in the parent widget. Consider the below example code with different values of x and y.
Example1:
Output:
Example 2:
Output:
Example 3:
Output:
- The application window has two text input fields and another one to display the result. There are four button objects with the captions Add, Subtract, Multiplication and Division. The user is expected to enter the number in the two Entry widgets. Their respective operation is displayed in the third.
- The first button (Add) is configured using the command parameter. Its value is the
add()
method in the class. The remaining buttons uses thebind()
method to register the left button click with thesub()
mul() div()
methods. All the methods read the contents of the text fields by theget()
method of the Entry widget, parse to numbers, perform the respective operation and display the result in third text field using theinsert()
method.
Output:
Download the project from the below mention link.