首页 > 解决方案 > 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()

标签: pythontkinter

解决方案


你应该把command=buttonClearinButton(...)而不是 in grid(...)

    clearButton = Button(second, text="Clear", fg="black", command=buttonClear)
    clearButton.grid(row=6, column=0, pady=5, columnspan=3)

推荐阅读