0

I'm writing my first GUI program today using Tkinter and I have stumbled onto a problem. I am trying to make a game that starts with an introduction window that closes after you press a button, then opens a new window where you can choose one of two modes. Unfortunately, I just can't get it running. It looks a little something like this.

#These are the functions that I defined to make it work
def start():
    root.destroy()

def Rules_mode_1():
    root.destroy
    rules1 = Tk()
    understood1 = Button(rules1, text="I understood", command="Start_game_mode_1")
    understood.pack()
    rules1.mainloop
# I haven't added rules 2 yet cause I couldn't get it to work with rules 1 so I haven't even #bothered but it would be the same code just switching the 1 for a 2. But really it isn't even 
#necessary to have 2 different rule functions because the rules are the same but I couldn't think
#of another way to go about it. if you have an idea let me know

def Start_game_mode_1(): 
    rules1.destroy #<----- THIS IS WHERE THE PROBLEM LIES. JUST DOESN'T RUN 
    gamemode1 = Tk()
    #Here I would have the game
    gamemode1.mainloop() 
#now same here don't have gamemode 2 yet cause it just doesn't work yet
#This is where it really starts    
root = Tk()

startbutton = Button(root, text="Start", command=start)
startbutton.pack

root.mainloop

root  = Tk()

def mode():
    mode1 = Button(root, command=Rules_mode_1)
    mode1.pack
    mode2 = #Buttonblablabla
mode()
root.mainloop()

Now I've been trying around for hours, trying to give the mainloops different names. For example giving the

rules1.mainloop
#the name
root.mainloop

but that obviously didn't work. I tried it with dozens of helper function and with the lambda expression and did hours of research but just can't seem to fix it. Does anybody have any ideas? Please be respectful and keep in mind it's my first time using Tkinter.

Thank you for your help!

  • 1
    Instead of creating two window instances, try to [stack multiple frames on top of another in a single window](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter?noredirect=1&lq=1) – Jdeep May 20 '21 at 04:41
  • When you call a function, you need to suffix the function name with `()`. Also `()` is needed when you define a function, like `def mode():`. – acw1668 May 20 '21 at 04:42
  • @Jdeep: the selected answer to that question is not a good place for a beginner to start. It ends up creating more questions than answers. – Bryan Oakley May 20 '21 at 04:56
  • `.mainloop()` and `.destroy()` are methods. If you write them without the parantheses in the end, they won't be executed. You should add parentheses (`()`) after them. Also, I agree with Jdeep that it would be better to use one big mainloop and just open and destroy different toplevel windows from it. That makes things way easier. – Martin Wettstein May 20 '21 at 14:01

1 Answers1

0

After the comments didn't really help me I just tried things out for hours and in case anybody ever is having a a similar problem and reads this: The rules1 variable is inside a function, and therefore only local, which means it can't be destroyed in another function. I fixed it by making it a global, like:

def Rules_mode_1():
    root.destroy
    global rules1
    rules1 = Tk()
    understood1 = Button(rules1, text="I understood", command="Start_game_mode_1")
    understood.pack()
    rules1.mainloop

After that I could destroy the mainloop in the next function.

Yun
  • 3,056
  • 6
  • 9
  • 28