0

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?

Êrelyn
  • 23
  • 1
  • 5
  • so when you call the open filename dialog, are you picking a file? – Elijah Nov 04 '18 at 21:22
  • Yes. Exiting the dialog without picking a file works fine – Êrelyn Nov 04 '18 at 21:47
  • Well `root.destroy()` will end the tk instance however you may need to use `quit()` to close the interpreter. Take a look at this post: [difference between root.destroy() and root.quit()?](https://stackoverflow.com/questions/2307464/what-is-the-difference-between-root-destroy-and-root-quit) – Mike - SMT Nov 05 '18 at 12:54
  • @mike-SMT I know. The problem I'm having is, that `quit()`, `exit()`, `sys.exit()`, `os._exit`,.... will all produce a softlock, when pyperclip is loaded while an instance of tkFileDialog is open. No matter what I do, the script will not exit. – Êrelyn Nov 05 '18 at 14:43
  • I do not see `import os` in your example anywhere. If you have not done so already you need to add `import os` at the top. – Mike - SMT Nov 05 '18 at 15:10
  • @Mike-SMT edited. Sorry, omitted that part for brevity. Again, it doesn't matter which way I try to exit the script/interpreter to produce a softlock. – Êrelyn Nov 05 '18 at 15:11
  • Can you explain what you mean by `softlock`? As it is now your code works on my end. – Mike - SMT Nov 05 '18 at 15:18
  • @Mike-SMT Strange, must have to to something with my setup then. Tested this on 2 machines. Softlock: Script won't exit but perform any operation queued before rasing SystemExit as expected, python-process will become unresponsive and has to be closed forcefully. – Êrelyn Nov 05 '18 at 15:27
  • I tested on Eclipse IDE, Python IDLE and with Command Prompt and all exit as expected. I tested both with `os._exit(0)` and `SystemExit`. – Mike - SMT Nov 05 '18 at 15:33
  • @Mike-SMT Just tested again in a virtualenv with only pyperclip, tkinter and dependencies installed, just to make sure. Same problem. I'm on Windows 10, if that makes any difference. Unfortunately, I'm out of ideas how to track it further, as no error whatsoever is thrown. Thanks for your help so far though! – Êrelyn Nov 05 '18 at 16:11
  • Well I do know that some virtualenv have problems that need to be addressed for python. Tkinter is installed by default with all windows distrobutions that I am aware of so do not try to install them again. However you might want to try and reinstall pyperclip or maybe a different version release of pyperclip. – Mike - SMT Nov 05 '18 at 16:19
  • @Mike-SMT Yes, Tkinter is installed per default, which leaves me trying different versions of pyperclip. I need to work out of a virtualenv, for other unrelated compability issues. For now, I'm not using pyperclip at all, it's useful, but not too hard to implement for yourself. Thanks again for your help. – Êrelyn Nov 05 '18 at 16:29
  • @Êrelyn if you do figure it be sure to come back and answer your own question. It would be good to know what to do in this situation. – Mike - SMT Nov 05 '18 at 16:31
  • I'm testing this on Windows 10 with Pyperclip 1.7.0 (the latest) on Python 2.7.15, and your code seems to work for me. – Al Sweigart Nov 27 '18 at 20:05

1 Answers1

0

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.

Êrelyn
  • 23
  • 1
  • 5