In this tutorial, you will learn about Python GUI Programming with Tkinter module and be able to develop Basic Python Applications like Calculators. I would like you to code along so that you will get better understanding of the tutorial.
I have divided this tutorial in two sub parts:
The first part contains all the basic info. I assume that you have some you’re already somewhat familiar with Python Programming Language. Additionally, you need to know about Object Oriented Programming. In case you are unfamiliar with Python, I highly recommend you to go through our Introduction to Python Programming Tutorial.
Introduction to python programming
Tkinter provides Python applications with an easy to program interface. Tkinter supports a collection of Tk widgets that supports most applications needs. Tkinter in python is the interface to Tk, GUI tool kit for Tcl/Tk. Tkinter comprises a number of components. _tkinter is the low-level interface to Tk libraries and is linked into Python.
From the Python release 1.5.2 Tkinter, Tk and Tcl are the part of the standard installation package.
Tkinter adds object oriented features to Tcl/Tk. We use the Tk widgets to apply an operation to a widget identifier as Tkinter refers widgets as objects and you can drive the functions of the widgets using object methods and their attributes.
These are some of the major Tkinter widgets which you will use frequently.
Now comes the fun part. Let’s code 😁
A “Hello world” program is necessary to illustrate the ease with which a language may be applied. So let’s follow the trend and start with it.
from tkinter import mainloop, Label myLabel = Label(text="Hello World!!\n My first GUI Application.", font=("Arial, 24"), height=10, bg = '#4e148c').pack() # call the mainloop mainloop()
from tkinter import Label, mainloop
myLabel = Label(text="Hello World!!\n My first GUI Application.", font=("Arial, 24"), height=10, bg = '#4e148c').pack()
mainloop()
Now let’s go through a good example to boost your understanding of the topic. The example will illustrate you various Python and Tkinter features. The finished application will view somewhat like this:
def frame(root, side): w = Frame(root) w.pack(side=side, expand=YES, fill=BOTH) return w def button(master, side, text, command=None): w = Button(master=master, text=text, command=command) w.pack(side=side, expand=YES, fill=BOTH) return w
class Calculator(Frame): def __init__(self): Frame.__init__(self) self.pack(expand=YES, fill=BOTH) self.master.title('Simple Calculator') self.master.iconname('calculator') display = StringVar() # Entry Place Entry(self, relief=SUNKEN, textvariable=display).pack(side=TOP, expand=YES, fill=BOTH)
class Calculator(Frame): def __init__(self): # ... Upper code snippet is not included ... display = StringVar() # Entry Place Entry(self, relief=SUNKEN, textvariable=display).pack(side=TOP, expand=YES, fill=BOTH)
class Calculator(Frame): def __init__(self): # ... upper code snippet not included ... for key in ('123', '456', '789', '-0.'): keyF = frame(self, TOP) # lambda w = display for char in key: # print(char) button(keyF, LEFT, char, lambda w = display, c = char:w.set(w.get() + c))
class Calculator(Frame): def __init__(self): # ... upper code snippet not included ... opsF = frame(self, TOP) for char in '/*-+=': if char == '=': btn = button(opsF, LEFT, char) btn.bind('<ButtonRelease-1>', lambda e, s=self, w=display: s.calc(w), '+') else: btn = button(opsF, LEFT, char, lambda w = display, c=char:w.set(w.get() +' '+c+' ')) clearF = frame(self, TOP) clear = button(clearF, LEFT, 'Clear', lambda w = display: w.set(''))
from tkinter import * def frame(root, side): w = Frame(root) w.pack(side=side, expand=YES, fill=BOTH) return w def button(master, side, text, command=None): w = Button(master=master, text=text, command=command) w.pack(side=side, expand=YES, fill=BOTH) return w class Calculator(Frame): def __init__(self): Frame.__init__(self) self.pack(expand=YES, fill=BOTH) self.master.title('Simple Calculator') self.master.iconname('calculator') display = StringVar() # Entry Place Entry(self, relief=SUNKEN, textvariable=display).pack(side=TOP, expand=YES, fill=BOTH) for key in ('123', '456', '789', '-0.'): keyF = frame(self, TOP) # lambda w = display for char in key: # print(char) button(keyF, LEFT, char, lambda w = display, c = char:w.set(w.get() + c)) opsF = frame(self, TOP) for char in '/*-+=': if char == '=': btn = button(opsF, LEFT, char) btn.bind('<ButtonRelease-1>', lambda e, s=self, w=display: s.calc(w), '+') else: btn = button(opsF, LEFT, char, lambda w = display, c=char:w.set(w.get() +' '+c+' ')) clearF = frame(self, TOP) clear = button(clearF, LEFT, 'Clear', lambda w = display: w.set('')) def calc(self, display): try: x = eval(display.get()) display.set(x) except ValueError: display.set('Error') if __name__ == '__main__': Calculator().mainloop()
Do like and comment below if you face any issue. I will be happy to help you. 😁
In Python, the print() function is a fundamental tool for displaying output. While printing simple…
Python is a versatile programming language known for its simplicity and flexibility. When working on…
PDF (Portable Document Format) files are commonly used for sharing documents due to their consistent…
PDF (Portable Document Format) files are widely used for document exchange due to their consistent…
Python is a high-level programming language known for its simplicity and ease of use. However,…
Object-Oriented Programming (OOP), iterators, generators, and closures are powerful concepts in Python that can be…
This website uses cookies.