/
spirzen
/
it-code-examples
Обзор
Документация
Войти
/
spirzen
/
it-code-examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/python/lab-1124-026/main.py
34 строки
1 KB
Spirzen
Code migration pack
09 июн 2026, 01:41
09 июн 2026, 01:41
cebc284
Код
Авторство
О чём код?
import tkinter as tk def append_digit(d): display_var.set(display_var.get() + d) def clear(): display_var.set("") def calculate(): try: display_var.set(str(eval(display_var.get()))) except Exception: display_var.set("Ошибка") root = tk.Tk() root.title("Калькулятор") root.resizable(False, False) display_var = tk.StringVar(value="") entry = tk.Entry(root, textvariable=display_var, justify="right", font=("Consolas", 16), width=14) entry.grid(row=0, column=0, columnspan=4, padx=8, pady=8, sticky="ew") buttons = [ ("7", 1, 0), ("8", 1, 1), ("9", 1, 2), ("/", 1, 3), ("4", 2, 0), ("5", 2, 1), ("6", 2, 2), ("*", 2, 3), ("1", 3, 0), ("2", 3, 1), ("3", 3, 2), ("-", 3, 3), ("0", 4, 0), ("C", 4, 1), ("=", 4, 2), ("+", 4, 3), ] for text, row, col in buttons: cmd = clear if text == "C" else calculate if text == "=" else lambda t=text: append_digit(t) tk.Button(root, text=text, width=5, height=2, command=cmd).grid(row=row, column=col, padx=2, pady=2) root.mainloop()