You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python+Tkinter计算器按钮文本显示与窗口缩放布局问题求助

Tkinter计算器问题修复方案

核心前置说明

你原来代码中window.mainloop()之后的所有命令行输入输出代码完全不会在GUI运行时执行,mainloop()是Tkinter的事件阻塞循环,只有关闭窗口后才会执行后续代码,因此需要将计算逻辑全部整合到GUI回调函数中。

1. 按钮点击内容显示到文本框实现

实现逻辑

  • 定义内容追加函数,点击数字/运算符按钮时将对应文本插入到文本框末尾
  • 单独定义清空、退格、计算函数绑定到对应功能按钮

修改位置

所有按钮创建时新增command参数绑定对应回调函数,在创建文本框之后、按钮之前定义回调函数即可。

2. 窗口缩放布局不重叠实现

优化方向

  • 弃用原来混用pack+place的布局方式,计算器属于典型网格布局,使用grid布局管理器更合适
  • 给按钮所在的行和列设置weight参数,缩放时自动按比例分配空间
  • 设置窗口最小尺寸,避免缩放到过小导致控件变形

修改位置

删除所有按钮和文本框的pack()、place()调用,改用grid()指定行列位置,在窗口初始化后配置行列权重。

完整修改后可运行代码

import tkinter as tk
from tkinter import messagebox

window = tk.Tk()
window.geometry('500x500')
window.title('Manoj Calculator')
# 设置窗口最小尺寸,防止缩放过度变形
window.minsize(300,400)
# 配置行列权重,实现自适应缩放
for i in range(7):
    window.grid_rowconfigure(i, weight=1)
for j in range(4):
    window.grid_columnconfigure(j, weight=1)

# 顶部欢迎标签
label = tk.Label(window, text = 'The calculator created by manoj welcomes you')
label.grid(row=0, column=0, columnspan=4, sticky='nsew', padx=5, pady=5)

# 文本框,改用Entry更适合单行输入
text_box = tk.Entry(window , bg = "#ffffff" , fg = "#0000ff", font=('Arial',20))
text_box.grid(row=1, column=0, columnspan=4, sticky='nsew', padx=5, pady=5)

# 回调函数:追加内容到文本框
def add_content(char):
    text_box.insert(tk.END, char)

# 回调函数:清空文本框
def clear_content():
    text_box.delete(0, tk.END)

# 回调函数:退格
def backspace():
    current = text_box.get()
    if current:
        text_box.delete(len(current)-1, tk.END)

# 回调函数:计算结果
def calculate():
    try:
        expr = text_box.get().replace('X','*')
        res = eval(expr)
        text_box.delete(0, tk.END)
        text_box.insert(0, str(res))
    except ZeroDivisionError:
        messagebox.showerror('错误','不能除以0')
        clear_content()
    except:
        messagebox.showerror('错误','表达式无效')
        clear_content()

# 按钮创建,改用grid布局
# 第一行按钮:C、/、*、-
btn_clear = tk.Button(window , text = "C" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=clear_content)
btn_clear.grid(row=2, column=0, sticky='nsew', padx=2, pady=2)

btn_div = tk.Button(window , text = "/" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('/'))
btn_div.grid(row=2, column=1, sticky='nsew', padx=2, pady=2)

btn_mul = tk.Button(window , text = "X" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('X'))
btn_mul.grid(row=2, column=2, sticky='nsew', padx=2, pady=2)

btn_minus = tk.Button(window , text = "-" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('-'))
btn_minus.grid(row=2, column=3, sticky='nsew', padx=2, pady=2)

# 第二行按钮:7、8、9、+
btn7 = tk.Button(window , text = "7" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('7'))
btn7.grid(row=3, column=0, sticky='nsew', padx=2, pady=2)

btn8 = tk.Button(window , text = "8" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('8'))
btn8.grid(row=3, column=1, sticky='nsew', padx=2, pady=2)

btn9 = tk.Button(window , text = "9" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('9'))
btn9.grid(row=3, column=2, sticky='nsew', padx=2, pady=2)

btn_plus = tk.Button(window , text = "+" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('+'))
btn_plus.grid(row=3, column=3, sticky='nsew', padx=2, pady=2)

# 第三行按钮:4、5、6、<-
btn4 = tk.Button(window , text = "4" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('4'))
btn4.grid(row=4, column=0, sticky='nsew', padx=2, pady=2)

btn5 = tk.Button(window , text = "5" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('5'))
btn5.grid(row=4, column=1, sticky='nsew', padx=2, pady=2)

btn6 = tk.Button(window , text = "6" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('6'))
btn6.grid(row=4, column=2, sticky='nsew', padx=2, pady=2)

btn_back = tk.Button(window , text = "<-" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=backspace)
btn_back.grid(row=4, column=3, sticky='nsew', padx=2, pady=2)

# 第四行按钮:1、2、3、=
btn1 = tk.Button(window , text = "1" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('1'))
btn1.grid(row=5, column=0, sticky='nsew', padx=2, pady=2)

btn2 = tk.Button(window , text = "2" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('2'))
btn2.grid(row=5, column=1, sticky='nsew', padx=2, pady=2)

btn3 = tk.Button(window , text = "3" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('3'))
btn3.grid(row=5, column=2, sticky='nsew', padx=2, pady=2)

btn_equ = tk.Button(window , text = "=" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 9, command=calculate)
btn_equ.grid(row=5, column=3, rowspan=2, sticky='nsew', padx=2, pady=2)

# 第五行按钮:.、0
btn_point = tk.Button(window , text = "." , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('.'))
btn_point.grid(row=6, column=0, sticky='nsew', padx=2, pady=2)

btn0 = tk.Button(window , text = "0" , fg = "#000000" , bg = "#ffffff" , height = 2 , width = 3, command=lambda: add_content('0'))
btn0.grid(row=6, column=1, columnspan=2, sticky='nsew', padx=2, pady=2)

window.mainloop()

内容的提问来源于stack exchange,提问作者Manoj Namburi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 22:54:03