-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.py
More file actions
112 lines (94 loc) · 3.61 KB
/
Copy pathconverter.py
File metadata and controls
112 lines (94 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import tkinter as tk
from tkinter import messagebox
# === Логика ===
def update_result(*_):
# Обновляет результат при каждом изменении числа или системы.
num_str = entry_var.get().strip()
if not num_str:
output_label.config(text="")
return
try:
input_base = input_base_var.get()
if input_base == "BIN":
num = int(num_str, 2)
elif input_base == "OCT":
num = int(num_str, 8)
elif input_base == "DEC":
num = int(num_str, 10)
elif input_base == "HEX":
num = int(num_str, 16)
else:
raise ValueError
# Всегда показывать из всех систем счисления
result_text = (
f"BIN: {bin(num)}\n"
f"OCT: {oct(num)}\n"
f"DEC: {num}\n"
f"HEX: {hex(num)}"
)
output_label.config(text=result_text)
except ValueError:
output_label.config(text="❌ Некорректное число для выбранной системы")
def add_digit(digit):
entry_var.set(entry_var.get() + str(digit))
def clear_entry():
entry_var.set("")
output_label.config(text="")
# === Интерфейс ===
root = tk.Tk()
root.title("Конвертер систем счисления")
root.geometry("420x560")
root.configure(bg="#1e1e1e")
root.resizable(False, False)
# --- Цвета ---
FG = "#ffffff"
BG = "#1e1e1e"
BTN_BG = "#333333"
BTN_ACTIVE = "#555555"
ENTRY_BG = "#2d2d2d"
RESULT_BG = "#111111"
RESULT_FG = "#00ffcc"
# --- Поле ввода ---
entry_var = tk.StringVar()
tk.Label(root, text="Введите число:", fg=FG, bg=BG, font=("Consolas", 12)).pack(pady=(10, 2))
entry = tk.Entry(root, textvariable=entry_var, font=("Consolas", 18), justify="center",
bg=ENTRY_BG, fg=FG, insertbackground=FG, relief="flat", width=24)
entry.pack(pady=5)
# --- Кнопки выбора системы ---
tk.Label(root, text="Система ввода:", fg=FG, bg=BG, font=("Consolas", 12)).pack(pady=(10, 2))
input_base_var = tk.StringVar(value="DEC")
frame_input_base = tk.Frame(root, bg=BG)
frame_input_base.pack()
for base in ["BIN", "OCT", "DEC", "HEX"]:
tk.Radiobutton(frame_input_base, text=base, variable=input_base_var, value=base,
fg=FG, bg=BG, selectcolor=BTN_BG, activebackground=BTN_ACTIVE,
font=("Consolas", 11), command=update_result).pack(side="left", padx=5)
# --- Поле вывода ---
tk.Label(root, text="Результаты:", fg=FG, bg=BG, font=("Consolas", 12)).pack(pady=(10, 2))
output_label = tk.Label(root, text="", font=("Consolas", 14),
bg=RESULT_BG, fg=RESULT_FG, justify="left",
width=36, height=5, anchor="nw", relief="flat", padx=10, pady=5)
output_label.pack(pady=10)
# --- Кнопки цифр ---
btn_frame = tk.Frame(root, bg=BG)
btn_frame.pack(pady=10)
buttons = [
["7", "8", "9"],
["4", "5", "6"],
["1", "2", "3"],
["0", "C"]
]
for row in buttons:
row_frame = tk.Frame(btn_frame, bg=BG)
row_frame.pack()
for text in row:
if text == "C":
cmd = clear_entry
else:
cmd = lambda t=text: add_digit(t)
tk.Button(row_frame, text=text, command=cmd, width=5, height=2,
bg=BTN_BG, fg=FG, activebackground=BTN_ACTIVE, relief="flat",
font=("Consolas", 12)).pack(side="left", padx=4, pady=4)
# === Автоматическое обновление при вводе ===
entry_var.trace_add("write", update_result)
root.mainloop()