python code to make a calculator frame

Solutions on MaxInterview for python code to make a calculator frame by the best coders in the world

showing results for - "python code to make a calculator frame"
Julián
08 Sep 2017
1# importing everyting from tkinter
2from tkinter import *
3# expression to access among all the functions
4expression = ""
5# functions
6def input_number(number, equation):
7   # accessing the global expression variable
8   global expression
9   # concatenation of string
10   expression = expression + str(number)
11   equation.set(expression)
12def clear_input_field(equation):
13   global expression
14   expression = ""
15   # setting empty string in the input field
16   equation.set("Enter the expression")
17def evaluate(equation):
18global expression
19# trying to evaluate the expression
20try:
21result = str(eval(expression))
22# showing the result in the input field
23equation.set(result)
24# setting expression to empty string
25expression = ""
26except:
27# some error occured
28# showing it to the user equation.set("Enter a valid expression")
29expression = ""
30# creating the GUI
31def main():
32   # main window window = Tk()
33   # setting the title of GUI window
34   window.title("Calculator")
35   # set the configuration of GUI window
36   window.geometry("325x175")
37   # varible class instantiation
38   equation = StringVar()
39   # input field for the expression
40   input_field = Entry(window, textvariable=equation)
41   input_field.place(height=100)
42   # we are using grid position
43   # for the arrangement of the widgets
44   input_field.grid(columnspan=4, ipadx=100, ipady=5)
45   # settin the placeholder message for users
46   equation.set("Enter the expression")
47   # creating buttons and placing them at respective positions
48   _1 = Button(window, text='1', fg='white', bg='black', bd=0, command=lambda: input_number(1, equation), height=2, width=7)
49   _1.grid(row=2, column=0)
50   _2 = Button(window, text='2', fg='white', bg='black', bd=0, command=lambda: input_number(2, equation), height=2, width=7)
51   _2.grid(row=2, column=1)
52   _3 = Button(window, text='3', fg='white', bg='black', bd=0, command=lambda: input_number(3, equation), height=2, width=7)
53   _3.grid(row=2, column=2)
54   _4 = Button(window, text='4', fg='white', bg='black', bd=0, command=lambda: input_number(4, equation), height=2, width=7)
55   _4.grid(row=3, column=0)
56   _5 = Button(window, text='5', fg='white', bg='black', bd=0, command=lambda: input_number(5, equation), height=2, width=7)
57   _5.grid(row=3, column=1)
58   _6 = Button(window, text='6', fg='white', bg='black', bd=0, command=lambda: input_number(6, equation), height=2, width=7)
59   _6.grid(row=3, column=2)
60   _7 = Button(window, text='7', fg='white', bg='black', bd=0, command=lambda: input_number(7, equation), height=2, width=7)
61   _7.grid(row=4, column=0)
62   _8 = Button(window, text='8', fg='white', bg='black', bd=0, command=lambda: input_number(8, equation), height=2, width=7)
63   _8.grid(row=4, column=1)
64   _9 = Button(window, text='9', fg='white', bg='black', bd=0, command=lambda: input_number(9, equation), height=2, width=7)
65   _9.grid(row=4, column=2)
66   _0 = Button(window, text='0', fg='white', bg='black', bd=0, command=lambda: input_number(0, equation), height=2, width=7)
67   _0.grid(row=5, column=0)
68   plus = Button(window, text='+', fg='white', bg='black', bd=0, command=lambda: input_number('+', equation), height=2, width=7)
69   plus.grid(row=2, column=3)
70   minus = Button(window, text='-', fg='white', bg='black', bd=0, command=lambda: input_number('-', equation), height=2, width=7)
71   minus.grid(row=3, column=3)
72   multiply = Button(window, text='*', fg='white', bg='black', bd=0, command=lambda:  input_number('*', equation), height=2, width=7)
73   multiply.grid(row=4, column=3)
74   divide = Button(window, text='/', fg='white', bg='black', bd=0, command=lambda: input_number('/', equation), height=2, width=7)
75   divide.grid(row=5, column=3)
76   equal = Button(window, text='=', fg='white', bg='black', bd=0, command=lambda: evaluate(equation), height=2, width=7)
77   equal.grid(row=5, column=2)
78   clear = Button(window, text='Clear', fg='white', bg='black', bd=0, command=lambda: clear_input_field(equation), height=2, width=7)
79   clear.grid(row=5, column=1)
80   # showing the GUI
81   window.mainloop()
82# start of the program
83if __name__ == '__main__':
84      main()
85
queries leading to this page
calculator in python tkinterpython gui calculator programgui calculator in pythonpython code for calculator guipython calculator tkinter guicreating a calculator in tkintercalculator in tkinter python addition calculator using python tkintercalculator with tkinterpython code calculator with guikalkulator gui pythoncalculator python guihow to make calculator in python tkinter full codecalculator program usin tkinter in pythoncreating tkinter gui calculatorcalculator in python guicreate calculator using any python guitkinter gui calculator with download pdfpython calculator appmaking a calculator in tkinterpython calculator guistrcutured package for calculator using pythonelectronics how to make calculator in pythongui calculator in python doubtdo calculations with tkinterhow to crreat a calculator in python with graphical user interfacecalculator using tkintercaculator in tkinterpython tkinter calculatorcalculator with python tkintercalculator in tkinter from sctracthow to make a calculator without display box tkintertkinter calculator python 3 codecalculator using tkinter for only additionhow to make graphical calculator pythobcalculator gui using tk intermake a compex calculator app in pythontkinter claculetordesign simple python gui calculatorcreate a calculator app with pythoncalculator in tkintercalculator in python with guihow to create calculator with tkintercreate calcultor in python projecttkinter python 3 windows gui calculator codesource code for gui calculator in pythontkinter calculatorhow to make a calculator tkinterhow to make a calculator in python with guicreate calculator in python using tkintercalculator gui projects in python create calculator app in pythonpython gui calculator codecreate calculator in tkinterbeautiful calculator using tkinkermake databse ui simple calculation codecalculator gui pythonhow to create a calculator gui in pythoncreate a calculator with tinktercalculator gui code in pythoncalculator using python tkintersimple add calculator in tkinter pythonwindow geometry calculator in pythonbuilding a gui calculatorpython program for gui calculatorhow to make a calculator in python tkintercalculator with gui in pythonhow to make a gui calculator in pythonpython simple calculator 28using tkinter 29how to make calculator layout in python using tkinterpython gui calculatortkinter code for calculatorcalculator python tkintersimple python code for calculator tkinterhow to make calculator in python tkintermake clculator with interface in pythoncalculator tkintersimple addition calculator in python using tkintercalculator guibuilding a calculator in tkinterhow to keep adding number in cacultor tkintersimple calculator gui in pythonpython code to make a calculator frame