furas

Tkinter Calculator

Feb 13th, 2020
167
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # question: https://stackoverflow.com/questions/60216042/problem-with-button-alignment-with-python-tkinter
  2. # image: https://imgur.com/gallery/AS5JsPQ
  3.  
  4. import tkinter as tk
  5.  
  6. # --- functions ---
  7.  
  8. def on_click(value):
  9.     result['text'] += value
  10.  
  11. # --- main ---
  12.  
  13. window = tk.Tk()
  14. window.geometry("800x600")
  15. window.title("Calculator")
  16.  
  17. menu = tk.Menu(window)
  18. new_item = tk.Menu(menu)
  19. new_item.add_command(label="Language", command=window.destroy)
  20. new_item.add_command(label="Reset", command=window.destroy)
  21. new_item.add_command(label="Save result as txt", command=window.destroy)
  22. new_item.add_command(label="Help", command=window.destroy)
  23. new_item.add_command(label="Quit", command=window.destroy)
  24. menu.add_cascade(label="Options", menu=new_item)
  25. window.config(menu=menu)
  26.  
  27. # ---
  28.  
  29. for c in range(1, 5):
  30.     window.columnconfigure(c, weight=1)
  31.  
  32. for r in range(1, 6):
  33.     window.rowconfigure(r, weight=1)
  34.  
  35. window['bg'] = 'black'
  36. grid_options = {'sticky': 'news', 'padx': 1, 'pady': 1}
  37.  
  38. # --- result ---
  39.  
  40. frame_result = tk.Frame(window, bg='red')
  41. frame_result.grid(column=1, row=1, columnspan=4, **grid_options)
  42.  
  43. result = tk.Label(frame_result, text="", bg="yellow", fg="black", cursor="exchange")
  44. result.pack(padx=20, pady=(20,20), fill='both', expand=True) # , pady=(20,70)
  45.  
  46. # --- numbers ---
  47.  
  48. options = {'bg': "green", 'activebackground': "darkgreen", 'fg': "white", 'relief': 'flat', 'highlightbackground': 'green'}#, 'highlightthickness': 0}
  49.  
  50. # -
  51. btn = tk.Button(window, text="7", command=lambda:on_click('7'), **options)
  52. btn.grid(column=1, row=2, **grid_options)
  53.  
  54. btn = tk.Button(window, text="8", command=lambda:on_click('8'), **options)
  55. btn.grid(column=2, row=2, **grid_options)
  56.  
  57. btn = tk.Button(window, text="9", command=lambda:on_click('9'), **options)
  58. btn.grid(column=3, row=2, **grid_options)
  59.  
  60. # -
  61.  
  62. btn = tk.Button(window, text="4", command=lambda:on_click('4'), **options)
  63. btn.grid(column=1, row=3, **grid_options)
  64.  
  65. btn = tk.Button(window, text="5", command=lambda:on_click('5'), **options)
  66. btn.grid(column=2, row=3, **grid_options)
  67.  
  68. btn = tk.Button(window, text="6", command=lambda:on_click('6'), **options)
  69. btn.grid(column=3, row=3, **grid_options)
  70.  
  71. # -
  72.  
  73. btn = tk.Button(window, text="1", command=lambda:on_click('1'), **options)
  74. btn.grid(column=1, row=4, **grid_options)
  75.  
  76. btn = tk.Button(window, text="2", command=lambda:on_click('2'), **options)
  77. btn.grid(column=2, row=4, **grid_options)
  78.  
  79. btn = tk.Button(window, text="3", command=lambda:on_click('3'), **options)
  80. btn.grid(column=3, row=4, **grid_options)
  81.  
  82. # -
  83.  
  84. btn = tk.Button(window, text="0", command=lambda:on_click('0'), **options)
  85. btn.grid(column=1, row=5, **grid_options)
  86.  
  87. options =  {'fg': "white", 'relief': 'flat'}#, 'highlightthickness': 0}
  88.  
  89. btn = tk.Button(window, text=",", command=lambda:on_click(','), bg="green", activebackground="darkgreen", highlightbackground='green', **options)
  90. btn.grid(column=2, row=5, **grid_options)
  91.  
  92. options =  {'fg': "black", 'relief': 'flat'}#, 'highlightthickness': 0}
  93.  
  94. btn = tk.Button(window, text="=", command=lambda:on_click('='), bg="yellow", activebackground="orange", highlightbackground='yellow', **options)
  95. btn.grid(column=3, row=5, **grid_options)
  96.  
  97. # --- other ---
  98.  
  99. options = {'bg': "lightgreen", 'activebackground': "lightblue", 'fg': "black", 'relief': 'flat', 'highlightthickness': 0}
  100.  
  101. btn = tk.Button(window, text="+", command=lambda:on_click('+'), **options)
  102. btn.grid(column=4, row=2, **grid_options)
  103.  
  104. btn = tk.Button(window, text="-", command=lambda:on_click('-'), **options)
  105. btn.grid(column=4, row=3, **grid_options)
  106.  
  107. btn = tk.Button(window, text="*", command=lambda:on_click('*'), **options)
  108. btn.grid(column=4, row=4, **grid_options)
  109.  
  110. btn = tk.Button(window, text="/", command=lambda:on_click('/'), **options)
  111. btn.grid(column=4, row=5, **grid_options)
  112.  
  113. # ---
  114.  
  115. window.mainloop()
RAW Paste Data