Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import messagebox
- import sys
- expression = ""
- def press(num):
- global expression
- expression = expression + str(num)
- equation.set(expression)
- def equalpress():
- try:
- global expression
- total = str(eval(expression))
- equation.set(total)
- expression = ""
- except:
- equation.set(" error ")
- expression = ""
- def clear():
- global expression
- expression = ""
- equation.set("")
- def quit():
- global expression
- gui.destroy()
- if __name__ == "__main__":
- gui = Tk()
- gui.title("Calculator")
- gui.geometry("200x250")
- ###FIX THIS######FIX THIS######FIX THIS######FIX THIS###
- backgroundImage = PhotoImage("C:\\Users\\erik\\Pictures\\bg.gif")
- equation = StringVar()
- expression_field = Entry(gui, textvariable=equation)
- expression_field.grid(columnspan=4, ipadx=70)
- equation.set('. . .')
- B1 = Button(gui, text=' 1 ', fg='Black', bg='White',
- command=lambda: press(1), height=1, width=7)
- B1.grid(row=2, column=0)
- B2 = Button(gui, text=' 2 ', fg='Black', bg='White',
- command=lambda: press(2), height=1, width=7)
- B2.grid(row=2, column=1)
- B3 = Button(gui, text=' 3 ', fg='Black', bg='White',
- command=lambda: press(3), height=1, width=7)
- B3.grid(row=2, column=2)
- B4 = Button(gui, text=' 4 ', fg='Black', bg='White',
- command=lambda: press(4), height=1, width=7)
- B4.grid(row=3, column=0)
- B5 = Button(gui, text=' 5 ', fg='Black', bg='White',
- command=lambda: press(5), height=1, width=7)
- B5.grid(row=3, column=1)
- B6 = Button(gui, text=' 6 ', fg='Black', bg='White',
- command=lambda: press(6), height=1, width=7)
- B6.grid(row=3, column=2)
- B7 = Button(gui, text=' 7 ', fg='Black', bg='White',
- command=lambda: press(7), height=1, width=7)
- B7.grid(row=4, column=0)
- B8 = Button(gui, text=' 8 ', fg='Black', bg='White',
- command=lambda: press(8), height=1, width=7)
- B8.grid(row=4, column=1)
- B9 = Button(gui, text=' 9 ', fg='Black', bg='White',
- command=lambda: press(9), height=1, width=7)
- B9.grid(row=4, column=2)
- B0 = Button(gui, text=' 0 ', fg='Black', bg='White',
- command=lambda: press(0), height=1, width=7)
- B0.grid(row=5, column=0)
- plus = Button(gui, text=' + ', fg='Black', bg='White',
- command=lambda: press("+"), height=1, width=7)
- plus.grid(row=2, column=3)
- minus = Button(gui, text=' - ', fg='Black', bg='White',
- command=lambda: press("-"), height=1, width=7)
- minus.grid(row=3, column=3)
- multiply = Button(gui, text=' * ', fg='Black', bg='White',
- command=lambda: press("*"), height=1, width=7)
- multiply.grid(row=4, column=3)
- divide = Button(gui, text=' / ', fg='Black', bg='White',
- command=lambda: press("/"), height=1, width=7)
- divide.grid(row=5, column=3)
- equal = Button(gui, text=' = ', fg='Black', bg='White',
- command=equalpress, height=1, width=7)
- equal.grid(row=5, column=2)
- clear = Button(gui, text='Clear', fg='Black', bg='White',
- command=clear, height=1, width=7)
- clear.grid(row=5, column=1)
- close = Button(gui, text= 'Quit', fg='Black', bg='White',
- command=quit, height=1, width=7)
- close.grid(row=7, column=1)
- # start the GUI
- gui.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement