简体   繁体   中英

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 ".

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

I am only having problems calling the functions from this window from " [ FinishButton ] " and " [ 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 .

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
  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"
  4. Avoid self-destructive practice to equip a .Button() with a power to commit suiccide by .destroy() + assignment to 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
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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