python - Python tkinter 嵌套函数在单独的窗口中
问题描述
使用 tkinter,我正在尝试制作一个数值方法计算器。我有一个起始窗口(根),其中有一个下拉菜单,为用户提供 3 个选项。当他们选择一个选项时,它会打开一个新窗口,要求用户输入,然后解决方法并显示答案。我还没有写数值方法部分,但我遇到的问题是我的插值窗口的清除按钮。我想制作一个清除该窗口中输入框的清除按钮,但它没有显示出来。
from tkinter import *
root = Tk()
root.title("Numerical Methods Calculator")
welcomeMessage = Label(root, text="Welcome! Are you ready to solve some numerical methods?")
welcomeMessage.grid(row=0, column=0, pady=20, columnspan=3)
def linear():
top = Toplevel()
top.title('Solve Linear Equation')
x1 = Entry(top)
x1.grid(row=0, column=0, padx=20, pady=20)
def polynomial():
third = Toplevel()
third.title('Solve Polynomial Roots')
def interpolation():
second = Toplevel()
second.title('Solve Interpolation')
instructions = Label(second, text="Please fill in five values, and leave one blank. Then click the solve button, and the last value will be solved.")
instructions.grid(row=0,column=0, pady=20, columnspan=3)
def buttonClear():
x1.delete(0, END)
x2.delete(0, END)
x3.delete(0, END)
y1.delete(0, END)
y2.delete(0, END)
y3.delete(0, END)
def solveButton():
return
xLabel = Label(second, text="x")
xLabel.grid(row=1, column=0, columnspan=3)
x1 = Entry(second)
x1.grid(row=2, column=0, columnspan=3, pady=1)
x2 = Entry(second)
x2.grid(row=3, column=0, columnspan=3, pady=1)
x3 = Entry(second)
x3.grid(row=4, column=0, columnspan=3, pady=1)
yLabel = Label(second, text="y")
yLabel.grid(row=1, column=1, columnspan=3)
y1 = Entry(second)
y1.grid(row=2, column=1, columnspan=3, pady=1)
y2 = Entry(second)
y2.grid(row=3, column=1, columnspan=3, pady=1)
y3 = Entry(second)
y3.grid(row=4, column=1, columnspan=3, pady=1)
myButton = Button(second, text="Solve", command=solveButton, fg="black")
myButton.grid(row=5, column=0, pady=5, columnspan=3)
clearButton = Button(second, text="Clear", fg="black")
clearButton.grid(row=6, column=0, pady=5, command=buttonClear, columnspan=3)
menu = Menu(root)
root.config(menu=menu)
equationMenu = Menu(menu)
menu.add_cascade(label="Choose Numerical Method", menu=equationMenu)
equationMenu.add_command(label="Solve Linear Equations", command=linear)
equationMenu.add_command(label="Solve Interpolation", command=interpolation)
equationMenu.add_command(label="Solve Polynomial Roots", command=polynomial)
root.mainloop()
解决方案
你应该把command=buttonClear
inButton(...)
而不是 in grid(...)
:
clearButton = Button(second, text="Clear", fg="black", command=buttonClear)
clearButton.grid(row=6, column=0, pady=5, columnspan=3)
推荐阅读
- c# - 从 Razor Pages 中的库生成 URL
- postgresql - Postgresql 如何加入这些表以获得正确的输出?
- php - 使用 replit.com 和 remotemysql.com
- python - 当我只想删除一个包时,为什么 conda 想更新不相关的包?
- reactjs - 如何在 PdfTron/Pdf.js(React App)中使用 loadDocument() 函数加载在文件夹内提供路径的文档
- python - 按行划分数据帧(或numpy数组)的正确方法
- sorting - 在 Arm 装配中实现选择排序时遇到问题
- html - 替换 Bootstap svg 占位符图像
- angular - 每当单击该表值时,Angular/PrimeNG 都会在索引/值处打开一个模式对话框
- mysql - 将 CIDR 格式的 IPv6 范围转换为 MySQL 中的 IPv6 地址范围(开始和结束 ips)