1

I'm trying to run this function as long as entry is 1 or 2. but after the first valid input (1 or 2) the second one doesn't do anything and seems to get stuck in the loop

(the function prints the chosen file name + the input number)

from tkinter import Tk
from tkinter.filedialog import askopenfilename
Tk().withdraw() # there's no need to open the full GUI

def my_filebrowser(command):
    filename = askopenfilename()
    print(filename, command)

while True:

    command = int(input('enter command: '))

    if command == 1:
        my_filebrowser(command)

    elif command == 2:
        my_filebrowser(command)

    else:
        break

how should I change the code in order to use the tkinter file browser (askopenfilename() function) multiple times?

Pouria
  • 95
  • 1
  • 7
  • Your while loop interrupts the mainloop. This means that the instance of `Tk()` cant get any input anymore. Either you call `update()` in your whileloop, which [isnt recommanded](https://stackoverflow.com/questions/66781579/when-should-i-use-root-update-in-tkinter-for-python/66781785#66781785) or you overthink your achitecture. Why do you do what you are doing? – Thingamabobs Nov 28 '21 at 09:15
  • Possible dublicate of https://stackoverflow.com/q/9319317/13629335 – Thingamabobs Nov 28 '21 at 09:19
  • @Thingamabobs the user supposed to open a file based on their command. but the program continues so the user can enter a command and choose a file again. is there an easier way to do this? looks like tkinter and loops are problematic when used together – Pouria Nov 28 '21 at 09:26
  • There are many questions to be answered before I personally would recommand an approach. Questions like, do you need a cross platform file dialog? Do you really need a high performance for your code? – Thingamabobs Nov 28 '21 at 09:33
  • @Thingamabobs there's no need of cross-platform and the program isn't that heavy for performance optimization. the only thing that matters is the ability to open file browser (file dialog) multiple times – Pouria Nov 28 '21 at 09:40

1 Answers1

1

You could simply create and destroy the root window to achive that, but it isnt a high performance approach.

from tkinter import Tk
from tkinter.filedialog import askopenfilename


def my_filebrowser(command):
    root = Tk(); root.withdraw()
    filename = askopenfilename()
    print(filename, command)
    root.destroy()

while True:

    command = int(input('enter command: '))

    if command == 1:
        my_filebrowser(command)

    elif command == 2:
        my_filebrowser(command)

    else:
        break

You can modify this code to have the filedialog in the front and in the middle of the screen. I also added alpha for transparency that you dosent even notice what happens:

def my_filebrowser(command):
    root = Tk(); root.attributes('-alpha',0.01)
    root.attributes('-topmost',True)
    root.tk.eval(f'tk::PlaceWindow {root._w} center')
    root.withdraw()
    filename = askopenfilename()
    root.destroy()
    print(filename, command)  
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54