From 3ea8193c64c7067bfc91e979ce81dd8c947e382a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8A?= <2295311501@qq.com> Date: Thu, 10 Oct 2024 17:16:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=9D=E5=A4=96=E8=A1=A5=E5=85=85=E4=BA=86?= =?UTF-8?q?=E5=8E=9F=E5=A7=8B=E7=89=88=E6=9C=AC=E7=9A=84=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=BF=9B=E5=88=B6=E8=BD=AC=E6=8D=A2.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 进制转换.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 进制转换.py diff --git a/进制转换.py b/进制转换.py new file mode 100644 index 0000000..dbe9ad1 --- /dev/null +++ b/进制转换.py @@ -0,0 +1,69 @@ +import tkinter as tk +from tkinter import ttk +from tkinter import messagebox + +# 进制转换函数 +def convert_base(): + try: + # 获取输入值和进制 + input_num = input_entry.get().strip() + input_base = int(input_base_combobox.get()) + output_base = int(output_base_combobox.get()) + + # 将输入数从指定进制转换为十进制 + decimal_num = int(input_num, input_base) + + # 将十进制数转换为目标进制 + if output_base == 10: + result = str(decimal_num) + else: + result = '' + digits = "0123456789ABCDEF" + while decimal_num > 0: + result = digits[decimal_num % output_base] + result + decimal_num //= output_base + if result == '': + result = '0' + + # 显示转换结果 + output_entry.config(state=tk.NORMAL) + output_entry.delete(0, tk.END) + output_entry.insert(0, result) + output_entry.config(state=tk.DISABLED) + + except ValueError: + messagebox.showerror("错误", "输入的数字格式不正确或超出进制范围") + +# 创建主窗口 +root = tk.Tk() +root.title("进制转换器") +root.geometry("400x250") + +# 输入框标签和输入框 +tk.Label(root, text="输入数字:").pack(pady=5) +input_entry = tk.Entry(root, width=30) +input_entry.pack(pady=5) + +# 输入进制选择 +tk.Label(root, text="输入数字的进制:").pack(pady=5) +input_base_combobox = ttk.Combobox(root, values=[str(i) for i in range(2, 17)], state="readonly", width=5) +input_base_combobox.current(0) # 默认选择2进制 +input_base_combobox.pack(pady=5) + +# 输出进制选择 +tk.Label(root, text="输出数字的进制:").pack(pady=5) +output_base_combobox = ttk.Combobox(root, values=[str(i) for i in range(2, 17)], state="readonly", width=5) +output_base_combobox.current(0) # 默认选择2进制 +output_base_combobox.pack(pady=5) + +# 转换按钮 +convert_button = tk.Button(root, text="转换", command=convert_base) +convert_button.pack(pady=10) + +# 输出框标签和输出框 +tk.Label(root, text="转换结果:").pack(pady=5) +output_entry = tk.Entry(root, width=30, state=tk.DISABLED) +output_entry.pack(pady=5) + +# 运行主循环 +root.mainloop()