Tkinter - Python 3.3 - 破坏窗口的问题

时间:2014-11-17 22:05:35

标签: python windows tkinter

我目前正在 tkinter 编写一个基本程序,并且遇到一个特定窗口的问题,称为“appetiser”。< / p>

上一个屏幕上的按钮。

我对我遇到的问题感到困惑所以我粘贴了下面的整个appetiser窗口。 (抱歉只是把它转储到这里)

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

现在

从其他窗口调用2个函数 OpenMain() OpenWindow() 以返回到之前的屏幕,但在其他窗口中调用它们时工作正常窗户。

我只是在“ [FinishButton] ”和“ [BackButton] ”中调用此窗口中的函数时遇到问题。< / p>

以下是之前调用过的2个函数,工作正常。

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

调用OpenFinish()函数后,“appetiser窗口不会被销毁,主窗口会直接在appetiser 窗口

就像我说的,抱歉只是在这里转储我的代码,但我无法弄清楚问题。

这些功能在其他窗口中运行得很好,我也遵循相同的格式为程序的其余部分创建窗口。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:-1)

  1. 避免使用global -s
  2. 基于对象/类的方式
  3. 设计
  4. 撰写对象&amp; 管理他们的对齐&amp; 控制严格限制在.Tk() / .Toplevel() / .Frame()“容器”的基于Tkinter的hiearachy中的内容“
  5. 避免自我毁灭的做法,使 .Button() 具备通过.destroy() +分配给self进行杀戮的权力

  6. #  <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
        ...