Advertisement
Guest User

laskin.py

a guest
Dec 12th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import messagebox
  3.  
  4. import sys
  5. expression = ""
  6.  
  7. def press(num):
  8.     global expression
  9.     expression = expression + str(num)
  10.     equation.set(expression)
  11.  
  12.  
  13. def equalpress():
  14.     try:
  15.         global expression
  16.         total = str(eval(expression))
  17.         equation.set(total)
  18.         expression = ""
  19.  
  20.     except:
  21.         equation.set(" error ")
  22.         expression = ""
  23.  
  24. def clear():
  25.     global expression
  26.     expression = ""
  27.     equation.set("")
  28.  
  29. def quit():
  30.     global expression
  31.     gui.destroy()
  32.  
  33. if __name__ == "__main__":
  34.     gui = Tk()
  35.     gui.title("Calculator")
  36.     gui.geometry("200x250")
  37.  
  38.     ###FIX THIS######FIX THIS######FIX THIS######FIX THIS###
  39.     backgroundImage = PhotoImage("C:\\Users\\erik\\Pictures\\bg.gif")
  40.  
  41. equation = StringVar()
  42.  
  43. expression_field = Entry(gui, textvariable=equation)
  44. expression_field.grid(columnspan=4, ipadx=70)
  45.  
  46. equation.set('. . .')
  47.  
  48. B1 = Button(gui, text=' 1 ', fg='Black', bg='White',
  49.                     command=lambda: press(1), height=1, width=7)
  50. B1.grid(row=2, column=0)
  51.  
  52. B2 = Button(gui, text=' 2 ', fg='Black', bg='White',
  53.                     command=lambda: press(2), height=1, width=7)
  54. B2.grid(row=2, column=1)
  55.  
  56. B3 = Button(gui, text=' 3 ', fg='Black', bg='White',
  57.                     command=lambda: press(3), height=1, width=7)
  58. B3.grid(row=2, column=2)
  59.  
  60. B4 = Button(gui, text=' 4 ', fg='Black', bg='White',
  61.                     command=lambda: press(4), height=1, width=7)
  62. B4.grid(row=3, column=0)
  63.  
  64. B5 = Button(gui, text=' 5 ', fg='Black', bg='White',
  65.                     command=lambda: press(5), height=1, width=7)
  66. B5.grid(row=3, column=1)
  67.  
  68. B6 = Button(gui, text=' 6 ', fg='Black', bg='White',
  69.                     command=lambda: press(6), height=1, width=7)
  70. B6.grid(row=3, column=2)
  71.  
  72. B7 = Button(gui, text=' 7 ', fg='Black', bg='White',
  73.                     command=lambda: press(7), height=1, width=7)
  74. B7.grid(row=4, column=0)
  75.  
  76. B8 = Button(gui, text=' 8 ', fg='Black', bg='White',
  77.                     command=lambda: press(8), height=1, width=7)
  78. B8.grid(row=4, column=1)
  79.  
  80. B9 = Button(gui, text=' 9 ', fg='Black', bg='White',
  81.                     command=lambda: press(9), height=1, width=7)
  82. B9.grid(row=4, column=2)
  83.  
  84. B0 = Button(gui, text=' 0 ', fg='Black', bg='White',
  85.                     command=lambda: press(0), height=1, width=7)
  86. B0.grid(row=5, column=0)
  87.  
  88. plus = Button(gui, text=' + ', fg='Black', bg='White',
  89.                 command=lambda: press("+"), height=1, width=7)
  90. plus.grid(row=2, column=3)
  91.  
  92. minus = Button(gui, text=' - ', fg='Black', bg='White',
  93.                 command=lambda: press("-"), height=1, width=7)
  94. minus.grid(row=3, column=3)
  95.  
  96. multiply = Button(gui, text=' * ', fg='Black', bg='White',
  97.                     command=lambda: press("*"), height=1, width=7)
  98. multiply.grid(row=4, column=3)
  99.  
  100. divide = Button(gui, text=' / ', fg='Black', bg='White',
  101.                     command=lambda: press("/"), height=1, width=7)
  102. divide.grid(row=5, column=3)
  103.  
  104. equal = Button(gui, text=' = ', fg='Black', bg='White',
  105.                 command=equalpress, height=1, width=7)
  106. equal.grid(row=5, column=2)
  107.  
  108. clear = Button(gui, text='Clear', fg='Black', bg='White',
  109.                 command=clear, height=1, width=7)
  110. clear.grid(row=5, column=1)
  111.  
  112. close = Button(gui, text= 'Quit', fg='Black', bg='White',
  113.                 command=quit, height=1, width=7)
  114. close.grid(row=7, column=1)
  115.     # start the GUI
  116.  
  117. gui.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement