如何控制 tkinter Toplevel windows 的显示?

How to control display of tkinter Toplevel windows?

这是我的代码的缩写形式。 在 [input] window 中,我希望它只弹出 [input2] window 而 [solve] window 将弹出在我按下按钮 Solve! 后向上。

但是目前[input2]和[solve]windows同时弹出解决! 按钮在单击时不执行任何操作。

这是我的代码:

from tkinter import *

class welcome():
    def __init__(self, master): #master: root - the main window
        # welcome and introduction
        self.master=master
        self.master.geometry("500x200")

        self.welcome = Label(self.master, text="Welcome to The Bees's")
        self.welcome.pack()
        self.intro = Label(self.master, text="This is our project for Python course")
        self.intro.pack()
        self.intro2 = Label(self.master, text="This program is use for solving Linear Programming, IP, BIP, mixed... problem")
        self.intro2.pack()

        self.startbutton = Button(self.master, text="Start", bg="yellow")
        self.startbutton.pack()
        self.startbutton.config(command=self.gotoinput)

    def gotoinput(self):
        root2 = Toplevel(self.master)
        self.input1=input(root2)

Class 输入 window:

class input():
    def __init__(self,master):
        self.master=master
        self.master.title("Input")
        self.master.geometry("700x500")
        #cancle
        self.Canclebutton=Button(self.master,text="Cancle",command=self.master.destroy).grid(row=3,column=1)
        #OK
        self.Enterbutton=Button(self.master,text="ENTER",command=self.gotoinput2).grid(row=3,column=2)

    def gotoinput2(self):
        root3=Toplevel(self.master)
        self.input22=input2(root3)

Class 用于输入 2 window:

class input2():
    def __init__(self,master):
        self.master=master
        self.master.title("Input2")
        self.master.geometry("700x500")
        # cancle
        self.Canclebutton = Button(self.master, text="Cancle", command=self.master.destroy).grid(row=0, column=0)
        # OK
        self.Solvebutton = Button(self.master, text="Solve!", command=self.gotosolve()).grid(row=0, column=1)
        print("Through")

    def gotosolve(self):
        print("gotosolve")
        root4=Toplevel(self.master)
        self.solve11=solve(root4)

Class 求解 window:

class solve():
    def __init__(self,master):
        self.master = master
        self.master.title("Solution")
        self.master.geometry("700x500")
        # cancle
        self.Canclebutton = Button(self.master, text="Cancle", command=self.master.destroy).grid(row=0,column=0)

主要功能:

def main():
    # create a main window
    root = Tk()
    root.title("The Bees's")

    b = welcome(root)
    # end line
    root.mainloop()

main()

正如评论中提到的 command=self.gotosolve() 是问题所在。应该是command=self.gotosolve

也就是说,我认为您应该以不同的方式处理这个问题。目前您正在创建一个顶层 window,然后将该顶层传递给 class。相反,您应该只使 class 从顶层继承。欢迎 class 也是如此。那可能应该只是继承自 Tk()。

看看我下面使用 Tk() 和 Toplevel classes 的示例。

import tkinter as tk


class Welcome(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("500x200")
        self.columnconfigure(0, weight=1)
        tk.Label(self, text="Welcome to The Bees's").grid(row=0, column=0)
        tk.Label(self, text="This is our project for Python course").grid(row=1, column=0)
        tk.Label(self, text="This program is use for solving Linear Programming, IP, BIP, mixed... problem").grid(row=2, column=0)
        tk.Button(self, text="Start", bg="yellow", command=lambda:Input(self)).grid(row=3, column=0)


class Input(tk.Toplevel):
    def __init__(self, master):
        tk.Toplevel.__init__(self, master)
        self.title("Input")
        self.geometry("700x500")
        tk.Button(self, text="Cancle", command=self.destroy).grid(row=3,column=1)
        tk.Button(self, text="ENTER", command=lambda: Input2(master)).grid(row=3,column=2)


class Input2(tk.Toplevel):
    def __init__(self, master):
        tk.Toplevel.__init__(self, master)
        self.title("Input2")
        self.geometry("700x500")
        tk.Button(self, text="Cancle", command=self.destroy).grid(row=0, column=0)
        tk.Button(self, text="Solve!", command=lambda: Solve(master)).grid(row=0, column=1)


class Solve(tk.Toplevel):
    def __init__(self, master):
        tk.Toplevel.__init__(self, master)
        self.master.title("Solution")
        self.master.geometry("700x500")
        tk.Button(self, text="Cancle", command=self.destroy).grid(row=0,column=0)


b = Welcome()

上面的示例将按您预期的那样工作,但我认为在打开下一个顶层之前关闭最后一个顶层可能会更好。真的不想让一堆 windows 在彼此之上打开。管理思路不难,喜欢的可以加。