0

I've been creating a game using Python, and in one of my functions,

def Enemy_Party(enemies, party):
        def Sub_Fight(member, enemy, enemies, Prep):
            Prep.destroy()
            Fight(member, enemy, enemies)
        for x in enemies:
            Prep = Tk()
            for y in party:
                button = Button(Prep, text=y.name, command=Sub_Fight(y, x, enemies, Prep)).pack()
            Prep.mainloop()

a single window is created, but with nothing in it. There is no error message, the blank window just sits there.

Yes, I have a function Fight, and it does take four arguments. Both 'enemies' and 'party' are defined as lists, and both have values within them (I've checked).

On that note, I have two questions: "Why does the program halt after creating one blank window?" and "Can you define windows in a 'For Loop'?

Let me know if you need any more code, and thanks ahead of time.

2
  • Your snippet does not help much to demonstrate what is happening. Can you provide minimal working example demonstrating the issue you have? Commented Feb 3, 2015 at 1:07
  • 1
    Also, this button = Button(Prep, text=y.name, command=Sub_Fight(y, x, enemies, Prep)).pack() will result in button being null. The reason is that pack returns null. Maybe this is part of the issue you are having? Commented Feb 3, 2015 at 1:10

2 Answers 2

3

The program halts the first time mainloop is called, because that's what mainloop does -- it doesn't return until you destroy the window.

You have a fundamental misunderstanding of how to create windows. Every tkinter application needs a single instance of Tk. To create additional windows you need to create instances of Toplevel. You then need to call mainloop exactly once.

In your code, replace calls to Tk with calls to Toplevel, then remove the call to mainloop in your function. Presumably you're calling mainloop somewhere else in your code after all of the widgets have been created.

Sign up to request clarification or add additional context in comments.

Comments

0

I notice that you're defining Tk() inside a for loop. Depending on how many times this loop iterates, you could be creating many instances of Tk() when actually you should never create more than one. I would advise creating your window outside of the loop and populating it as you need to using the loop (or whatever else you may decide to use).

4 Comments

I was actually trying to create multiple windows, one for each 'y in party'. Is that possible? If not, can I create a Frame for each 'y'?
Yeah, you can have multiple windows, but only ONE "master" window (Tk()). Any additional windows need to be defined using Toplevel() instead. I.E., Prep = Toplevel(). You may need to check this, but I'm also pretty sure each window needs a unique name, so they can't ALL be "Prep".
Thanks, that worked perfectly! . P.S. The program managed to create two windows named Prep using the loop.
Ok, nice one. I wasn't sure about the naming thing so good to hear that works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.