-1

I'm trying to make it so that when a file pathname is valid, it will switch frames to the next menu. The issue is the validation only runs when the class is frame is initialised. Help?

class PlaySong(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="Play Song").pack(side="top", fill="x", pady=10)
        tk.Label (self,text= r"Enter in path of MIDI file (e.g C:\Users\etc.)").pack(side="top", fill="x", pady=10)
        self.SongPath = tk.Entry(self)
        self.SongPath.pack()
        valid = False
        self.enter = tk.Button(self, text="Enter path", command = lambda: valid == self.SongCheck)
        self.enter.pack(pady=10)
        tk.Button(self, text="Return to main menu",fg="red2", command=lambda: master.switch_frame(MainMenu)).pack(fill="x")
        if valid == True:
            master.switch_frame(PlaySongMixer)

    def SongCheck(self):
        path = self.SongPath.get()
        try:
            pygame.mixer.music.load(path)
        except:
            self.enter.config(text="Invalid path. Try again.")
        else:
            return True
Will
  • 25
  • 6
  • 2
    The variable `valid` in a `lambda` statement, `command = lambda: valid == self.SongCheck)`. is local and does not change the allready destroyed local `__init__.valid = False`. Read up on [scopes-and-namespaces](https://docs.python.org/3/tutorial/classes.html#scopes-and-namespaces-example) and First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) – stovfl Feb 11 '20 at 11:48
  • Thanks! Unfortunately it still isn't switching to the next frame, even with valid as a global variable, any advice on how to fix this? – Will Feb 11 '20 at 14:42
  • ***"even with valid as a global variable,"***: Now, you have a `global value == True` after click `["Enter path"]`. But how do you expect to benefit from that? Why don't you use the same syntax as with `["Return to main menu"]`? – stovfl Feb 11 '20 at 16:46

1 Answers1

0

You should call switch_frame() inside SongCheck() function:

class PlaySong(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        tk.Label(self, text="Play Song").pack(side="top", fill="x", pady=10)
        tk.Label(self, text=r"Enter in path of MIDI file (e.g C:\Users\etc.)").pack(side="top", fill="x", pady=10)

        self.SongPath = tk.Entry(self)
        self.SongPath.pack()

        self.enter = tk.Button(self, text="Enter path", command=self.SongCheck)
        self.enter.pack(pady=10)

        tk.Button(self, text="Return to main menu", fg="red2", command=lambda: master.switch_frame(MainMenu)).pack(fill="x")

    def SongCheck(self):
        path = self.SongPath.get()
        try:
            pygame.mixer.music.load(path)
            self.master.switch_frame(PlaySongMixer)
        except Exception as e:
            self.enter.config(text="Invalid path. Try again.")
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Thanks! For some reason, even though it doesn't show an error, it doesn't switch the frame. For clarification, this is an initial class for forming a Window, which then switches to different menus using switch_frame. – Will Feb 12 '20 at 09:14
  • Never mind, it was an issue with the modules! Thank you so much! – Will Feb 12 '20 at 09:20
  • That means something wrong on `pygame.mixer.music.load(path)`. – acw1668 Feb 12 '20 at 09:20