Advertisement
Guest User

Calculator

a guest
Dec 7th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. from tkinter import (Tk, BOTH, Button, Entry,
  2.     Event, Frame, NSEW, RIGHT, StringVar)
  3.  
  4. ## -- START OF CONFIG --
  5.  
  6. DISPLAY_FONT = ("Arial", 24)
  7. BUTTON_FONT = ("Arial", 16)
  8. WINDOW_DIMENSIONS = "512x384"
  9.  
  10. ## -- END OF CONFIG --
  11.  
  12. ## Creating the window.
  13. window = Tk()
  14. window.geometry(WINDOW_DIMENSIONS)
  15.  
  16. ## Creating the main frame to expand with resizing.
  17. frame = Frame(window)
  18.  
  19. ## Setting up the statement display.
  20. stream = StringVar(frame, name="stream")
  21.  
  22. entry = Entry(frame, textvariable=stream, font=DISPLAY_FONT, justify=RIGHT,
  23.     state="readonly", readonlybackground="Blue", foreground="White")
  24. entry.grid(row=0, column=0, columnspan=5, sticky=NSEW)
  25.  
  26. ## Defining functions for handling all key creation and callbacks.    
  27. def handle_key(event):
  28.     if event.keysym == "Escape" or event.char == "CLR":
  29.         window.call("set", "stream", "")
  30.     elif event.keysym == "Return" or event.char == '=':
  31.         try:
  32.             window.call("set", "stream", eval(stream.get().replace('x', '*')))
  33.         except:
  34.             window.call("set", "stream", "ERROR")
  35.     elif event.keysym == "BackSpace":
  36.         window.call("set", "stream", stream.get()[:-1])
  37.     elif event.char == '*':
  38.         window.call("append", "stream", 'x')
  39.     else:
  40.         window.call("append", "stream", event.char)
  41.  
  42. def add_button(key, row, column, binding, **kwargs):
  43.     event = Event()
  44.     event.keysym, event.char = "", key
  45.     button = Button(frame, text=key, font=BUTTON_FONT,
  46.         command=lambda: handle_key(event))
  47.     button.grid(row=row, column=column, sticky=NSEW, **kwargs)
  48.     window.bind(binding, handle_key)
  49.  
  50. def construct_row_column(num):
  51.     row, column = divmod(num, 3)
  52.     return (row + 1, column - 1) if column != 0 else (row, column + 2)
  53.  
  54. ## Adding number keys into the frame.
  55. for i in range(1, 10):
  56.     row, column = construct_row_column(i)
  57.     add_button(str(i), row, column, str(i))
  58.  
  59. ## Adding operation keys into the frame.
  60. add_button('x', 1, 3, '*')
  61. add_button('+', 2, 3, '+')
  62. add_button('-', 3, 3, '-')
  63.  
  64. add_button('CLR', 4, 0, "<Escape>")
  65. add_button('0', 4, 1, '0')
  66. add_button('.', 4, 2, '.')
  67. add_button('=', 4, 3, "<Return>", columnspan=2)
  68.  
  69. add_button('/', 1, 4, '/')
  70. add_button('(', 2, 4, '(')
  71. add_button(')', 3, 4, ')')
  72.  
  73. window.bind("<BackSpace>", handle_key)
  74.  
  75. ## Configuring cells to autoscale.
  76. frame.pack(fill=BOTH, expand=True)
  77. frame.grid_columnconfigure((0, 1, 2, 3, 4), weight=1)
  78. frame.grid_rowconfigure((0, 1, 2, 3, 4), weight=1)
  79.  
  80. ## Run the frame.
  81. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement