Ryan Brehmer
Ryan Brehmer

Reputation: 1

GUI doesn't close when i hit the button

I want it so that the number changes to let the GUI open and close, but it only opens a new 1, and it won't close.

here's the code:

from tkinter import *

screen = Tk()

InOpen = 1

def onclick(event):
    if event == 1:
        InOpen = 2
        InFrame = Frame(screen, width=500, height=350, bg="black")
        InFrame.pack()
    if event == 2:
        InFrame.destroy()
        InOpen = 1

mST = Label(screen, text="Minecraft", bg="grey", fg="white")
mST.pack(fill=X)

topFrame = Frame(screen)
topFrame.pack()
bottomFrame = Frame(screen)
bottomFrame.pack(side=BOTTOM)

button1 = Button(topFrame, text="Inventory", fg="blue", command=lambda:onclick(InOpen))
button2 = Button(topFrame, text="Combat", fg="red")

button1.pack(side=LEFT)
button2.pack(side=LEFT)

screen.mainloop()

Upvotes: 0

Views: 35

Answers (1)

Armali
Armali

Reputation: 19375

The shown code doesn't work just because in the function onclick the variables InOpen and InFrame are local, not global as intended. We have to declare that globals are meant:

def onclick(event):
    global InOpen, InFrame
    …

Upvotes: 1

Related Questions