1

这是我的代码的简短形式。在 [ input] 窗口中,我希望它只弹出 [ ] 窗口,点击按钮后会弹出input2[ ] 窗口。solveSolve!

但是,目前,[ input2] 和 [ solve] 窗口同时弹出,并且Solve!按钮在单击时不执行任何操作。

这是我的代码:

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 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)

input2 窗口的类:

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 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()
4

1 回答 1

0

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

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

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

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()

上面的示例将按您的预期工作,但是我认为在打开下一个之前关闭最后一个顶层可能会更好。真的不想让一堆窗户在彼此之上打开。这并不难管理思想,因此您可以根据需要添加它。

于 2018-06-19T16:02:05.193 回答