Support Forums
[Tutorial] Basic Tkinter Calculator - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Python Programming Language (https://www.supportforums.net/forumdisplay.php?fid=32)
+---- Thread: [Tutorial] Basic Tkinter Calculator (/showthread.php?tid=4915)



[Tutorial] Basic Tkinter Calculator - uber1337 - 02-20-2010

Hello, today I will be showing you some Tkinter basics and how to make a basic calculator. Tkinter comes with every distribution of Python and is the Python standard GUI kit. Lets get started:

First, we need to import Tkinter(obviously) this is done like this:
Code:
from Tkinter import *

Ok now we will use classes to code our program, first we need to define the function for our program to run when it first starts, __init__.
Code:
class calc(Frame):
    def __init__(self):
Before we start coding our widgets, we have to do this:
Code:
top = Tk()
        top.title('UberCalc v1.0')
        Frame.__init__(self)
This tells Tkinter that we want the title of our program to be UberCalc v1.0 (change it to whatever your want) and that the main function for our main window is __init__.
Ok now lets create some widgets!
Code:
global ent1, ent2, result
        Label(text='First Number').pack()
        ent1 = Entry()
        ent1.pack()
        Label(text='Second Number').pack()
        ent2 = Entry()
        ent2.pack()
        Label(text='Result').pack()
        result = Entry()
        result.pack()
TIP: You need to "pack" your widgets so that our program know where to put them.

First we define some global variables to be changed in other functions, then we make 3 entry box's(One for the 1st number, another for the 2ns number, and a 3rd for the result) and then we label them clearly. Ok so all we have now are some empty Entry box's and some Labels, thats no fun!
Lets make some buttons!:
Code:
plus = Button(self,  text='+', command=self.plus).pack(side=LEFT)
        minus = Button(self,  text='-',  command=self.minus).pack(side=LEFT)
        multi = Button(self,  text='*',  command=self.multiply).pack(side=LEFT)
        divide = Button(self,  text='/',  command=self.divide).pack(side=LEFT)
        clear = Button(self, text='Clear', command=self.clear).pack(side=LEFT)
Ok now we have 5 self explanatory buttons, they will be packed along side the bottom of our program. Now as you can see, the command for these buttons need to be defined, lets start with plus:
Code:
def plus(self):
        global ent1, ent2, result
First we define our global variables, now what we need to do is fetch whatever is in the entry box's when the button is pressed. Then we need to convert it into a float because anything in an entry box is a string by default.
Code:
first = float(ent1.get())
        second = float(ent2.get())
Ok, now we need to define our answer and insert it into the Result box, after clearing it of course.
Code:
answer = first + second
        result.delete(0, END)
        result.insert(0, str(answer))
Note that the insert function needs to insert a string.
Now we essentially do the same for all of our other functions, just different operations
Minus:
Code:
def minus(self):
        global ent1, ent2, result
        first = float(ent1.get())
        second = float(ent2.get())
        answer = first - second
        result.delete(0, END)
        result.insert(0, str(answer))
Multiply:
Code:
def multiply(self):
        global ent1, ent2, result
        first = float(ent1.get())
        second = float(ent2.get())
        answer = first * second
        result.delete(0, END)
        result.insert(0, str(answer))
Divide:
Code:
def divide(self):
        global ent1, ent2, result
        first = float(ent1.get())
        second = float(ent2.get())
        answer = first / second
        result.delete(0, END)
        result.insert(0, str(answer))
And now our clear function will simply delete whatever is in all of the text box's, like this:
Code:
def clear(self):
        global ent1, ent2, result
        ent1.delete(0, END)
        ent2.delete(0, END)
        result.delete(0, END)
Finally, we tell our program to start the main function if it is run, not imported
Code:
if __name__ == '__main__':
    window = calc()
    window.pack()
    window.mainloop()
And this is our final source:
Code:
class calc(Frame):
    def __init__(self):
        top = Tk()
        top.title('UberCalc v1.0')
        Frame.__init__(self)
        global ent1, ent2, result
        Label(text='First Number').pack()
        ent1 = Entry()
        ent1.pack()
        Label(text='Second Number').pack()
        ent2 = Entry()
        ent2.pack()
        Label(text='Result').pack()
        result = Entry()
        result.pack()
        plus = Button(self,  text='+', command=self.plus).pack(side=LEFT)
        minus = Button(self,  text='-',  command=self.minus).pack(side=LEFT)
        multi = Button(self,  text='*',  command=self.multiply).pack(side=LEFT)
        divide = Button(self,  text='/',  command=self.divide).pack(side=LEFT)
        clear = Button(self, text='Clear', command=self.clear).pack(side=LEFT)
    def plus(self):
        global ent1, ent2, result
        first = float(ent1.get())
        second = float(ent2.get())
        answer = first + second
        result.delete(0, END)
        result.insert(0, str(answer))
    def minus(self):
        global ent1, ent2, result
        first = float(ent1.get())
        second = float(ent2.get())
        answer = first - second
        result.delete(0, END)
        result.insert(0, str(answer))
    def multiply(self):
        global ent1, ent2, result
        first = float(ent1.get())
        second = float(ent2.get())
        answer = first * second
        result.delete(0, END)
        result.insert(0, str(answer))
    def divide(self):
        global ent1, ent2, result
        first = float(ent1.get())
        second = float(ent2.get())
        answer = first / second
        result.delete(0, END)
        result.insert(0, str(answer))
    def clear(self):
        global ent1, ent2, result
        ent1.delete(0, END)
        ent2.delete(0, END)
        result.delete(0, END)

The look will be different for each operating system but it should look something like this:
[Image: screeny1.jpg]