0% found this document useful (0 votes)
152 views

Tkinter Python Calculator Code

This document defines a Python class called Example that creates a basic graphical user interface (GUI) calculator app using Tkinter. The class initializes the GUI window and menu, defines functions for number buttons and arithmetic operations, and places the buttons, display, and clear button on the window. It then calls the main function to initialize the root window and run the GUI application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

Tkinter Python Calculator Code

This document defines a Python class called Example that creates a basic graphical user interface (GUI) calculator app using Tkinter. The class initializes the GUI window and menu, defines functions for number buttons and arithmetic operations, and places the buttons, display, and clear button on the window. It then calls the main function to initialize the root window and run the GUI application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

from tkinter import *

import tkinter.messagebox

class Example(Frame):

def __init__(self, parent):

Frame.__init__(self, parent, background="black")

self.parent = parent

self.initUI()

def initUI(self):

self.parent.title("Calculator")

self.pack(fill=BOTH, expand=1)

menubar = Menu(self.parent)

self.parent.config(menu=menubar)

Menu1 = Menu(menubar)

menubar.add_cascade(label="Options",font='nyala 14',menu=Menu1)

Menu1.add_command(label="Calculator",font='nyala 14',command=self.calculatorform)

# Menu1.add_command(label="History",font='nyala 14',command=self.historyform)

def destroy_children(self):

for child in self.winfo_children():

child.destroy()

def calculatorform(self):
self.destroy_children()

def enterfn(number):

n = screen.get()

screen.delete(0,END)

screen.insert(0,n+str(number))

def decimalfn(point):

n = screen.get()

screen.delete(0,END)

screen.insert(0,n+point)

def addfn():

f1 = screen.get()

global firstno

firstno = float(f1)

global operation

operation = "addition"

screen.delete(0,END)

def subtractfn():

f1 = screen.get()

global firstno

firstno = float(f1)

global operation

operation = "subtraction"

screen.delete(0,END)
def multiplyfn():

f1 = screen.get()

global firstno

firstno = float(f1)

global operation

operation = "multiplication"

screen.delete(0,END)

def dividefn():

f1 = screen.get()

global firstno

firstno = float(f1)

global operation

operation = "division"

screen.delete(0,END)

def equalfn():

secondno = float(screen.get())

screen.delete(0,END)

if (operation == "addition"):

ans = firstno + secondno

screen.insert(0,ans)

elif (operation == "subtraction"):

ans = firstno - secondno

screen.insert(0,ans)

elif (operation == "multiplication"):


ans = firstno * secondno

screen.insert(0,ans)

elif (operation == "division"):

ans = firstno / secondno

screen.insert(0,ans)

def clearfn():

screen.delete(0,END)

no0=Button(self,text="0",command=lambda:enterfn(0),bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

no0.pack()

no0.place(x=50, y=300)

no1=Button(self,text="1",command=lambda:enterfn(1),bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

no1.pack()

no1.place(x=50, y=250)

no2=Button(self,text="2",command=lambda:enterfn(2),bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

no2.pack()

no2.place(x=150, y=250)

no3=Button(self,text="3",command=lambda:enterfn(3),bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

no3.pack()
no3.place(x=250, y=250)

no4=Button(self,text="4",command=lambda:enterfn(4),bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

no4.pack()

no4.place(x=50, y=200)

no5=Button(self,text="5",command=lambda:enterfn(5),bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

no5.pack()

no5.place(x=150, y=200)

no6=Button(self,text="6",command=lambda:enterfn(6),bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

no6.pack()

no6.place(x=250, y=200)

no7=Button(self,text="7",command=lambda:enterfn(7),bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

no7.pack()

no7.place(x=50, y=150)

no8=Button(self,text="8",command=lambda:enterfn(8),bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

no8.pack()

no8.place(x=150, y=150)
no9=Button(self,text="9",command=lambda:enterfn(9),bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

no9.pack()

no9.place(x=250, y=150)

decimal=Button(self,text="●",command=lambda:decimalfn("."),bg='beige',fg='black'

,activebackground='peachpuff',font='nyala 14',width=5)

decimal.pack()

decimal.place(x=150, y=300)

plus=Button(self,text="+",command=addfn,bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

plus.pack()

plus.place(x=250, y=300)

minus=Button(self,text="-",command=subtractfn,bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

minus.pack()

minus.place(x=350, y=250)

times=Button(self,text="×",command=multiplyfn,bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)

times.pack()

times.place(x=350, y=200)

divide=Button(self,text="÷",command=dividefn,bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14',width=5)
divide.pack()

divide.place(x=350, y=150)

equal=Button(self,text="=",command=equalfn,bg='orange',fg='beige',

activebackground='peachpuff',font='nyala 14',width=5)

equal.pack()

equal.place(x=350, y=300)

screen=Entry(self,font='nyala 14')

screen.pack()

screen.place(x=50,y=50, width= 360, height = 50)

clear=Button(self,text="clear",command=clearfn,bg='beige',fg='black',

activebackground='peachpuff',font='nyala 14', width=10)

clear.pack()

clear.place(x=50, y=370)

def main():

root = Tk()

root.geometry("460x440+100+200")

app = Example(root)

root.mainloop()

if __name__ == '__main__':

main()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy