Why is everything freezing when I'm using Tkinter's file dialogue windows?

Viewed 25

Sorry this is so long but I'm trying to be detailed.

I found a similar question, but it was related to an import that I'm not using (so was the solution, so I'm not sure how relevant it is). The TL;DR is that I'm learning Tkinter and learning how to open files with it, but no matter what I do it's freezing.

The longer version is this - I'm trying to make an image opening program. Nothing fancy, just press a button to pull up the file dialogue screen and then open an image. The only other button in there is the exit button that works fine.

I made the whole thing and it mostly worked except for the aforementioned freezing (meaning it would actually display the image). I'm using PyCharm and it freezes so bad that I have to force quit Python and PyCharm.

Things I've tried:

  • Stripping away parts of the code to see what appears to be causing the freezing. That code is below.
  • Tested another little practice program with similar functionality (just an image viewer that lets the user cycle through images in a predetermined folder with no file dialog). It doesn't seem to be a problem with how I'm making buttons or anything since everything works in that program.

The issue in the code is at the line starting with "filedialog" and ending with the triple parentheses. If I "open" an image, nothing should happen, but even if I make it successfully open something, it will freeze anyway. I won't be able to close it with the "Exit" button and I won't be able to force quit it through PyCharm. I have to actually stop it through the Task Manager.

Here's the stripped down code that only has the dialog opening and the exit button:

from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog

root = Tk()
root.title('File dialogue')

filedialog.askopenfilename(initialdir='/image/', title='Pick an image', filetypes=(('png files', '*.png'), ('all files', '*.*')))

Button(root, text='Exit', command=root.quit).pack()

root.mainloop()
0 Answers
Related