Skip to content

Instantly share code, notes, and snippets.

@sqbi-q
Created October 2, 2022 20:49
Show Gist options
  • Save sqbi-q/a64dc0b72566eb089f9b42b435b95137 to your computer and use it in GitHub Desktop.
Save sqbi-q/a64dc0b72566eb089f9b42b435b95137 to your computer and use it in GitHub Desktop.
Prosty kalkulator GUI
from tkinter import *
from tkinter import ttk
'''
Zamysł okna:
---------------------- inputy, do których wpisywane
' ' będą wartości (ttk.Entry())
------- / -------- / ------
| ..... v ........ v .... |
| . ________ ________ . <----- frame przechowywujący inputy
| . |______| |______| . | (ttk.Frame())
| ....................... |
| | <-- okienko główne, obiekt Tk()
| ....................... | przypisany do zmiennej root
| . ____ ____ ____ ____ . |
| . |__| |__| |__| |__| . <----- frame przechowywujący
| ... ^ .. ^ .. ^ .. ^ .. | guziki (ttk.Frame())
|____ \ __ \ __ \ __ | ___|
. . . .
------------------------ guziki, po wciśnieciu których
wartości będą przeliczane na
odpowiadające guziką operacje
(ttk.Button())
'''
# tutorial widgetów Tk: https://tkdocs.com/tutorial/widgets.html
# metoda grid(): https://tkdocs.com/shipman/grid.html
# metody entry: https://tkdocs.com/pyref/ttk_entry.html
root = Tk()
frame_input = ttk.Frame(root, padding=10)
frame_input.grid()
frame_buttons = ttk.Frame(root, padding=10)
frame_buttons.grid()
# INPUTS
entry_a = ttk.Entry(frame_input)
entry_a.grid(column=0, row=0)
entry_b = ttk.Entry(frame_input)
entry_b.grid(column=1, row=0)
#
# BUTTONS
def clear_entry(entry, str=""):
entry.delete(0, END)
entry.insert(0, str)
def addition():
result = float(entry_a.get()) + float(entry_b.get())
clear_entry(entry_a, str(result))
def subtract():
result = float(entry_a.get()) - float(entry_b.get())
clear_entry(entry_a, str(result))
def multiply():
result = float(entry_a.get()) * float(entry_b.get())
clear_entry(entry_a, str(result))
def divide():
result = float(entry_a.get()) / float(entry_b.get())
clear_entry(entry_a, str(result))
ttk.Button(frame_buttons, text="+", command=addition).grid(column=0, row=0)
ttk.Button(frame_buttons, text="-", command=subtract).grid(column=1, row=0)
ttk.Button(frame_buttons, text="*", command=multiply).grid(column=2, row=0)
ttk.Button(frame_buttons, text="/", command=divide).grid(column=3, row=0)
#
root.mainloop() # run window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment