python gui calculator

Solutions on MaxInterview for python gui calculator by the best coders in the world

showing results for - "python gui calculator"
Sandro
20 Apr 2018
1from tkinter import *
2
3
4def adding():
5    try:
6        text1 = int(Textbox1.get())
7        text2 = int(Textbox2.get())
8    except Exception:
9        Output.delete(0, END)
10        Output.insert(0, 'Error! Enter a number please!')
11        return
12    text_output = str(text1 + text2)
13    Output.delete(0, END)
14    Output.insert(0, text_output)
15
16root = Tk()
17root.title('Adding')
18root.geometry('500x500')
19Textbox1 = Entry(root)
20Textbox1.pack(ipadx=50, ipady=10)
21spacing = Label(root, text='+')
22spacing.pack()
23Textbox2 = Entry(root)
24Textbox2.pack(ipadx=50, ipady=10)
25spacing2 = Label(root)
26spacing2.pack()
27Button1 = Button(root, text='Add The numbers!', command=adding)
28Button1.pack()
29spacing3 = Label(root)
30spacing3.pack()
31Output = Entry(root)
32Output.pack(ipadx=50)
33root.mainloop()
34
Simón
19 Nov 2016
1from tkinter import *
2 
3class Calculator:
4    def __init__(self,master):
5        self.master = master
6        master.title("My Calculator @ www.pickupbrain.com")
7        master.configure(bg='#C0C0C0')
8         
9        #creating screen widget
10        self.screen = Text(master, state='disabled', width=50,height= 3, background="#EAFAF1", foreground="#000000",font=("Arial",15,"bold"))
11         
12        #Screen position in window
13        self.screen.grid(row=0,column=0,columnspan=4,padx=2,pady=2)
14        self.screen.configure(state='normal')
15         
16        #initialize screen value as empty
17        self.equation=''
18         
19        #create buttons
20        b1 = self.createButton(7)
21        b2 = self.createButton(8)
22        b3 = self.createButton(9)
23        b4 = self.createButton(u"\u232B",None)
24        b5 = self.createButton(4)
25        b6 = self.createButton(5)
26        b7 = self.createButton(6)
27        b8 = self.createButton(u"\u00F7")
28        b9 = self.createButton(1)
29        b10 = self.createButton(2)
30        b11 = self.createButton(3)
31        b12 = self.createButton('*')
32        b13 = self.createButton('.')
33        b14 = self.createButton(0)
34        b15 = self.createButton('+')
35        b16 = self.createButton('-')
36        b17 = self.createButton('=', None,35)
37         
38        #stored all buttons in list
39        buttons = [b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17]
40         
41        #initalize counter
42        count=0
43         
44        #arrange buttons with grid manager
45        for row in range(1,5):
46            for column in range(4):
47                buttons[count].grid(row = row, column= column,padx=0,pady=0)
48                count +=1
49                 
50                #arrange last button '=' at the bottom
51            buttons[16].grid(row=5,column=0,columnspan=4)
52                 
53    def createButton(self,val,write=True,width=8):
54         
55        #this function creates a button and takes one compulsary argument, the value that should be on the button
56        return Button(self.master,text=val,command= lambda:self.click(val,write),width=width,background="#ffffff",foreground ="#1f4bff",font=("times",20,"bold"))
57                 
58    def click(self,text,write):
59        #this function handles the actions when you
60        #click a button 'write' arguement, if true
61        #than value val should be written on screen,
62        #if none then should not be written on screen
63        if write == None:
64             
65            #Evaluates when there is an equation to be evaluated
66            if text == '=' and self.equation:
67                #replace the unicode values of division ./. with python division
68                #symbol / using regex
69                self.equation = re.sub(u"\u00F7", '/', self.equation)
70                print(self.equation)
71                answer = str(eval(self.equation))
72                self.clear_screen()
73                self.insert_screen(answer,newline = True)
74            elif text == u"\u232B":
75                self.clear_screen()
76             
77        else:
78            #add text to screen
79                self.insert_screen(text)
80                 
81    def clear_screen(self):
82        #to clear screen
83        #set equation to empty before deleteing screen
84        self.equation = ''
85        self.screen.configure(state = 'normal')
86        self.screen.delete('1.0', END)
87                     
88    def insert_screen(self, value, newline = False):
89        self.screen.configure(state ='normal')
90        self.screen.insert(END,value)
91         
92        #record every value inserted in screen
93        self.equation += str(value)
94        self.screen.configure(state = 'disabled')
95                     
96def calci():
97     
98    #Function that creates calculator GUI
99    root = Tk()
100    my_gui = Calculator(root)
101    root.mainloop()
102     
103# Running Calculator    
104calci()                                    
105