简体   繁体   English

Tkinter-Python 3.3-销毁窗口的问题

[英]Tkinter - Python 3.3 - Problems destroying a window

I am currently writing a basic program in tkinter and having problems with one specific window , known as the " appetiser ". 我目前正在用tkinter编写一个基本程序,并且对一个特定的窗口 (称为“ appetiser ”)有问题。

A button on a previous screen. 前一个屏幕上的按钮。

I am really confused with the problem I am having so I have pasted the whole appetiser window below. 我真的对自己遇到的问题感到困惑,所以我在下面粘贴了整个appetiser窗口。 (sorry to just dump it here) (对不起,只是将其丢到这里)

#Appetiser Window
def appetiser():
    global app, root


#----------------------------------MEAL-1------------------------
#----------------------------------MEAL-1------------------------
    def PlusMix():
        global MixQuan
        MixQuan = MixQuan + 1
        DisplayButton["text"]=str(MixQuan)

    def NegMix():
        global MixQuan
        MixQuan = MixQuan - 1
        DisplayButton["text"]=str(MixQuan)

    Label(app, text = "", width = 65, height = 5).grid(row = 1, column = 0, sticky = N)

    Label(app,text = "Mixed Starter", font = ("roboto", 20)).grid(row = 2, column = 1, sticky = W)

    DisplayButton = Button(app, text = MixQuan, bg="magenta")
    DisplayButton.grid(column = 3, row = 2, sticky = W)
    DisplayButton.config(height = 10, width = 10 )

    Plus1Button = Button(app, text = "+1", command=PlusMix, bg="magenta")
    Plus1Button.grid(column = 4, row = 2, sticky = W)
    Plus1Button.config(height = 10, width = 10 )

    Neg1Button = Button(app, text = "-1", command=NegMix, bg="magenta")
    Neg1Button.grid(column = 2, row = 2, sticky = W)
    Neg1Button.config(height = 10, width = 10 )
#----------------------------------MEAL-2-------------------------
#----------------------------------MEAL-2-------------------------

    def PlusDuck():
        global DuckQuan
        DuckQuan = DuckQuan + 1 
        Display2Button["text"]=str(DuckQuan)

    def NegDuck():
        global DuckQuan
        DuckQuan = DuckQuan - 1 
        Display2Button["text"]=str(DuckQuan)



    Label(app,text = "Crispy Duck", font = ("roboto", 20)).grid(row = 3, column = 1, sticky = W)


    Display2Button = Button(app, text = DuckQuan, bg="magenta")
    Display2Button.grid(column = 3, row = 3, sticky = W)
    Display2Button.config(height = 10, width = 10 )

    Plus2Button = Button(app, text = "+1", command=PlusDuck, bg="magenta")
    Plus2Button.grid(column = 4, row = 3, sticky = W)
    Plus2Button.config(height = 10, width = 10 )

    Neg2Button = Button(app, text = "-1", command=NegDuck, bg="magenta")
    Neg2Button.grid(column = 2, row = 3, sticky = W)
    Neg2Button.config(height = 10, width = 10 )

#----------------------------------MEAL-3---------------------------
#----------------------------------MEAL-3---------------------------

    def plus3():
        global LambQuan
        LambQuan = LambQuan + 1 
        Display3Button["text"]=str(LambQuan)

    def neg3():
        global LambQuan
        LambQuan = LambQuan - 1 
        Display3Button["text"]=str(LambQuan)

    Label(app,text = "Lamb starter", font = ("roboto", 20)).grid(row = 4, column = 1, sticky = W)

    Display3Button = Button(app, text = LambQuan, bg="magenta")
    Display3Button.grid(column = 3, row = 4, sticky = W)
    Display3Button.config(height = 10, width = 10 )

    Plus3Button = Button(app, text = "+1", command=plus3, bg="magenta")
    Plus3Button.grid(column = 4, row = 4, sticky = W)
    Plus3Button.config(height = 10, width = 10 )

    Neg3Button = Button(app, text = "-1", command=neg3, bg="magenta")
    Neg3Button.grid(column = 2, row = 4, sticky = W)
    Neg3Button.config(height = 10, width = 10 )

#----------------------------------------------------
#----------------------------------------------------

    BackButton = Button(app, text = "Back", command=OpenMain, bg="magenta")
    BackButton.grid(column = 2, row = 5, sticky = W)
    BackButton.config(height = 10, width = 10 )


    FinishButton = Button(app, text = "Finish", command=OpenFinish, bg="magenta")
    FinishButton.grid(column = 4, row = 5, sticky = W)
    FinishButton.config(height = 10, width = 10 )


    app = Frame(root)
    app.grid()

    Label(app, text = "", width = 75, height = 20).grid(row = 1, column = 0, sticky = N)

    root.mainloop()

Now 现在

the 2 functions OpenMain() and OpenWindow() are called from other windows to return to previous screens but they work fine when being called in other windows. 从其他窗口调用了2个函数OpenMain()OpenWindow()以返回到先前的屏幕,但是在其他窗口中调用它们时,它们可以正常工作。

I am only having problems calling the functions from this window from " [ FinishButton ] " and " [ BackButton ] ". 我只有从“ [ FinishButton ] ”和“ [ BackButton ] ”从此窗口调用函数时遇到问题。

Below are the 2 functions which have been previously called and work fine. 以下是先前已调用且可以正常运行的两个函数。

#Wipe window and open main
def OpenMain():
    global app, root
    app.destroy()
    app = Frame(root) 
    app.grid()
    main()

#Wipe appetiser window and draw finish window
def OpenFinish():   
    global app, root
    app.destroy()
    app = Frame(root) 
    app.grid()
    FinishWindow()

When the OpenFinish() function is called, the " appetiser " window , is not destroyed and the main window is created directly under the appetiser window . OpenFinish()函数被调用时,“ appetiser窗口 ,不被破坏,并直接下创建的主窗口 appetiser 窗口

Like I said, sorry for just dumping my code here, but I just cant figure out the problem. 就像我说的那样,对不起,我只是将代码转储到这里,但是我无法弄清楚问题所在。

The functions work just fine in other windows, and I've followed the same format for creating windows for the rest of the program. 这些功能在其他窗口中也能正常工作,并且我遵循了相同的格式来为程序的其余部分创建窗口。

Any help would be much appreciated! 任何帮助将非常感激!

  1. Avoid global -s 避免global -s
  2. Design in object- / Class-based manner 设计在对象- /基于类的方式
  3. Compose objects & manage their alignment & control their content strictly inside a Tkinter-based hiearachy of .Tk() / .Toplevel() / .Frame() "containers" 撰写对象& 管理他们的对准及严格控制的基于TKinter-hiearachy内其内容.Tk() / .Toplevel() / .Frame()容器”
  4. Avoid self-destructive practice to equip a .Button() with a power to commit suiccide by .destroy() + assignment to self 避免自我毁灭性的练习,以使.Button()有权通过.destroy() +对self赋值来self

#  <appetiser> is not a window, it is a procedure ( a segment of code )
#
def appetiser():
    global app, root
    ...
    BackButton   = Button( app,
                           text    = "Back",
                           command = OpenMain,             # --> app.destroy()-->
                           bg      = "magenta"
                           )
    ...
    FinishButton = Button( app,
                           text    = "Finish",
                           command = OpenFinish,           # --> app.destroy()-->
                           bg      = "magenta"
                           )
    ...
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
#                                                                               v
def OpenMain():
    global app, root
    app.destroy()                                          # <-- COMMIT SUICCIDE
    app = Frame( root )                                    # new Frame instance
    app.grid()
    ...

def OpenFinish():   
    global app, root
    app.destroy()                                          # <-- COMMIT SUICCIDE
    app = Frame( root )                                    # new Frame instance
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM