Rotemika
Rotemika

Reputation: 61

Calling filedialog from inside a class Tkinter

I am trying to write an app in tkinter python that allows you to browse and choose a folder. I am using a class for each frame in my app, for example: BrowseFolders frame. In one of my frames I have a button that calls a command that opens filedialog.askdirectory. However, everytime I push the button the app seems to freeze and I have no other option other than closing it.

This is my class (notice the self.selectFolder_button):

class BrowseFolders(tk.Frame):
    def __init__(self, container):
        super().__init__(container, bg='#232228')
        self.path = ''

        # big title
        pyglet.font.add_file('ARCENA.ttf')
        self.Mp3Scanner_label = tk.Label(self, text="MP3 Scanner", font=('AR CENA', 70),
                                         background='#232228', foreground='#E3EEF9')
        self.slogan_label = tk.Label(self, text='Upload your files to MongoDB', font=('AR CENA', 30),
                                     background='#232228', foreground='#E3EEF9')
        self.selectFolder_button = tk.Button(self, text="Select Folder", font=('AR CENA', 15),
                                             background='grey', command=self.select_folder)

        self.pack()

    def select_folder(self):
        self.path = filedialog.askdirectory(initialdir='/', title="Select Folder")

    def pack(self):
        self.Mp3Scanner_label.place(relx=0.5, rely=0.35, anchor='center')
        self.slogan_label.place(relx=0.5, rely=0.55, anchor='center')
        self.selectFolder_button.place(relx=0.5, rely=0.85, relwidth=0.9, relheight=0.08, anchor='center')
        super().pack(expand=True, fill='both')


 class App(tk.Tk):
     def __init__(self):
         super().__init__()
         self.title('MP3 Scanner')
         self.geometry('600x450')


if __name__ == "__main__":
    app = App()
    BrowseFolders(app)
    app.mainloop()

I've already tried using lambda, changing my select_folder() method to be static, writing the method outside my class. Anything I do where the button somehow calls this function freezes my app. The only case where this does work is when my frame isn't a class.

Upvotes: 1

Views: 405

Answers (1)

Rotemika
Rotemika

Reputation: 61

Thank you very much! The problem really was the font, I removed pyglet.font.add_file('ARCENA.ttf') and now its working!

Upvotes: 1

Related Questions