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/ 4
Class- Comp A
Roll no.- 3148
Batch- B3
# Python program to create a simple GUI
# calculator using Tkinter
# import everything from tkinter module
from tkinter import *
# globally declare the expression variable
expression = ""
# Function to update expression
# in the text entry box def press(num): # point out the global expression variable global expression
# concatenation of string expression = expression + str(num)
# update the expression by using set method
equation.set(expression)
# Function to evaluate the final expression
def equalpress(): # Try and except statement is used # for handling the errors like zero # division error etc.
# Put that code inside the try block
# which may generate the error try:
global expression
# eval function evaluate the expression
# and str function convert the result # into string total = str(eval(expression))
equation.set(total)
# initialize the expression variable
# by empty string expression = ""
# if error is generate then handle
# by the except block except:
equation.set(" error ")
expression = ""
# Function to clear the contents
# of text entry box def clear(): global expression expression = "" equation.set("")
# Driver code if __name__ == "__main__": # create a GUI window gui = Tk()
# set the background colour of GUI window
gui.configure(background="light green")
# set the title of GUI window
gui.title("Simple Calculator")
# set the configuration of GUI window
gui.geometry("270x150")
# StringVar() is the variable class
# we create an instance of this class equation = StringVar()
# create the text entry box for
# showing the expression . expression_field = Entry(gui, textvariable=equation)
# grid method is used for placing
# the widgets at respective positions # in table like structure . expression_field.grid(columnspan=4, ipadx=70)
# create a Buttons and place at a particular
# location inside the root window . # when user press the button, the command or # function affiliated to that button is executed . button1 = Button(gui, text=' 1 ', fg='black', bg='blue', command=lambda: press(1), height=1, width=7) button1.grid(row=2, column=0)