-1

A part of my program needs to be able to play MIDI files, so I have an Entry in tkinter that is meant to allow the user to enter the path of one, but for some reason other subroutines within the class won't work with variables inside the class. Any ideas?

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)
        SongPath = tk.Entry(self)
        SongPath.pack()
        tk.Button(self, text="Enter path", command = self.SongCheck).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()
        print(path)
Will
  • 25
  • 6
  • Read up on [Python Classes and Objects, Section "The self"](https://www.geeksforgeeks.org/python-classes-and-objects/) – stovfl Feb 09 '20 at 18:55
  • Does this answer your question? [What is the purpose of the word 'self', in Python?](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self-in-python) – ChrisGPT was on strike Feb 09 '20 at 19:05

1 Answers1

1

SongPath is defined as a local variable in the __init__ method. To made this variable an attribute, write

self.SongPath = tk.Entry(self)

Do note that the naming conventions you are using for instance vairables conflicts with PEP-8. The common convention is to use snake_case for instance variables.

Brian61354270
  • 8,690
  • 4
  • 21
  • 43