-1

I'm making a pokemon game for school. First I have the starting game screen called login and from this screen the user has to press the button and that button being pressed triggers a new window menu to be created and the old login screen to be closed. This happens but in the main code out of the play(): function when I try to add an image label to the new screen menu it says menu is undefined. If anyone could help that would be greatly appreciated. My intention is to not use classes as we have yet to learn that in class for far.

from tkinter import *
from tkinter import messagebox

login = Tk()
login.title("Pokemon")
login.geometry('1000x750')
login.resizable(False, False)

background_image=PhotoImage(file='background.png')
background_label = Label(login, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

title = PhotoImage(file='title.png')

lbl = Label(login, image = title)
lbl.place(x=25, y=20)

messagebox.showinfo('WELCOME TO MY POKEMON GAME', 'HOPE YOU HAVE A GREAT TIME :)')

def play():
    global menu
    global login
    menu = Tk()
    menu.title("Pokemon Main Menu")
    menu.geometry('1000x750')
    menu.resizable(False, False)
    login.destroy()

begin= PhotoImage(file='begin.png')


btn = Button(login, image=begin, command = play)
btn.place(x=75, y=300)

background_image2=PhotoImage(file='background2.png')
background_label2 = Label(menu, image=background_image2)
background_label2.place(x=0, y=0, relwidth=1, relheight=1)


menu.mainloop()
login.mainloop()

Here's the Error I keep Getting receiving

 Traceback (most recent call last):
  File "/Users/ishaan/Desktop/attachments/Pokemon.py", line 39, in <module>
    background_label2 = Label(menu, image=background_image2)
NameError: name 'menu' is not defined
  • GUIs works different then you expect. `mainloop()` starts program and show window. All before `mainloop()` is executed before you see windows. So you try to add label before you click `Play` which create `menu` - you should do it inside `play()` - change indentations. – furas Jan 08 '20 at 19:59
  • Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) and [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) – stovfl Jan 08 '20 at 20:27
  • It looks like this may be a matter of incorrect indentation/scoping issues, and possibly lack of knowledge of the library. The questions which @stovfl linked show that there are already answers on the subject available, so I'm voting to close this. – AMC Jan 08 '20 at 20:57

1 Answers1

0

Your problem are wrong indentations.

You have to create label inside play() but you do it outside play() so it is executed at start, not when you press button, so it tries to add label before it creates menu

from tkinter import *
from tkinter import messagebox

#--- functions ---

def play():
    global menu
    global login

    login.destroy()

    menu = Tk()
    menu.title("Pokemon Main Menu")
    menu.geometry('1000x750')
    menu.resizable(False, False)

    background_image2 = PhotoImage(file='background2.png')
    background_label2 = Label(menu, image=background_image2)
    background_label2.place(x=0, y=0, relwidth=1, relheight=1)

    menu.mainloop()

# --- main ---

login = Tk()
login.title("Pokemon")
login.geometry('1000x750')
login.resizable(False, False)

background_image = PhotoImage(file='background.png')
background_label = Label(login, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

title = PhotoImage(file='title.png')
lbl = Label(login, image=title)
lbl.place(x=25, y=20)

btn = Button(login, command=play)
btn.place(x=75, y=300)

messagebox.showinfo('WELCOME TO MY POKEMON GAME', 'HOPE YOU HAVE A GREAT TIME :)')

login.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148