tkFileDialog and pyperclip prevent script from exiting

77 views Asked by At

A script with both tkFileDialog and pyperclip imported won't exit. (Python2.7) Working examples, where my script exits as expected:

import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
root.destroy()

As well as:

import pyperclip
print ('whatever')

Yet the following will prevent my script from exiting (raise SystemExit added for emphasis):

import Tkinter, tkFileDialog
import pyperclip
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
root.destroy()
raise SystemExit

Just importing both modules works fine, a tkFileDialog must be opened in order to create the error. Calling os._exit() or any code that raises SystemExit soft-locks the interpreter or the python-process, when called as a script.
It seems, that the problem occurs when pyperclip is loaded when opening a tkFileDialog, since the following fragment works as expected:

import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
root.destroy()
import pyperclip
raise SystemExit

In any case, though, every line of code after the critical part is executed as expected, raising SystemExit will create a soft-lock though.
This can't be used as a workaround though since python doesn't allow unloading of modules.

What am I doing wrong? Any ideas for a workaround?

1

There are 1 answers

0
Êrelyn On BEST ANSWER

Not a real solution, but the best I could come up with: Switching from python 2.7 to python 3.7 (and therefore from Tkinter 8.5 to 8.6) does the trick for me.

Of course, this has a lot of other implications, that I couldn't test.

On a sidenote - since others couldn't replicate the issue: I got the chance to run my code-snippet on yet another Windows 10-machine - it worked flawlessly with the same setup. So the problem definitely has something to do with the underlying system, not pyperclip or Tkinter itself.