0

I have a piece of code that gives me a bit of trouble. When I run it, I expect it to run the filedialog.askopenfilename only. However it also opens a small Tk window on the top-left corner of the screen. I am not sure why since there is nothing in my code (that I know of) calling for such Tk window.

Could you explain this for me?

from getpass import getuser
from sys import platform
from tkinter import filedialog
import os

userID = getuser()

try:
    if platform == "linux" or platform == "linux2":
        userpath = os.path.join("/", "home", userID, "Checklist_PDFs")
        print(userpath)
        filedialog.askopenfilename(initialdir=userpath, title="Select a file")

    elif platform == "darwin":
        userpath = os.path.join("/", "home", userID, "Checklist_PDFs")
        print(userpath)

    elif platform == "win32":
        userpath = os.path.join("c:", "\\", "Users", userID, "My Documents", "Checklist_PDFs")
        print(userpath)
except:
    print("My note: cannot execute")
Robin
  • 339
  • 1
  • 9
  • I got this, thank you for telling me how to prevent the second window to open. However, I am trying to understand what part of my code prompted the Tk window to open. Is it the filedialog function that creates a Tk window? – Robin Aug 23 '21 at 20:15
  • I just posted an answer, added some explanation there – Matiiss Aug 23 '21 at 20:16

1 Answers1

1

The easiest is to do sth like this (create a Tk instance and immediately .withdraw() it, that way it won't pop up, the reason it happens is that filedialog.askopenfilename()(or any other such window) requires a master, if there is none, it will create one. The reason that is the case is because there should be only one instance of Tk, so all these additional windows are most likely based on Toplevel() which requires a master, but they can be created as much as needed since they don't require their own .mainloop() or sth):

from tkinter import Tk

root = Tk()
root.withdraw()

So in your code it would look like this:

from getpass import getuser
from sys import platform
from tkinter import filedialog, Tk
import os


root = Tk()
root.withdraw()

userID = getuser()

try:
    if platform == "linux" or platform == "linux2":
        userpath = os.path.join("/", "home", userID, "Checklist_PDFs")
        print(userpath)
        filedialog.askopenfilename(initialdir=userpath, title="Select a file")

    elif platform == "darwin":
        userpath = os.path.join("/", "home", userID, "Checklist_PDFs")
        print(userpath)

    elif platform == "win32":
        userpath = os.path.join("c:", "\\", "Users", userID, "My Documents", "Checklist_PDFs")
        print(userpath)
except:
    print("My note: cannot execute")

Also I suggest that you handle the exceptions like this (if you don't know what to expect):

except Exception as e:
    print(e)

This will print out the exception, so you know what exception exactly was raised (and will also comply with PEP 8 or sth, or at least my IDE itself doesn't like too broad exception clauses)

Matiiss
  • 5,970
  • 2
  • 12
  • 29