Einfacher Dateidialog in Python
Manchmal möchte man innerhalb des Python-Skripts einen Dialog anzeigen, um den User z.B. eine Datei auswählen zu lassen. Mit TKinter geht das in Python recht einfach, den Fokus auf den Dialog zu setzen (ihn in den Vordergrund zu holen) ist dabei aber sehr sinnvoll. Folgender Code von Stackexchange (https://stackoverflow.com/questions/3375227/how-to-give-tkinter-file-dialog-focus) tut genau das:
import tkinter as tk from tkinter import filedialog # Make a top-level instance and hide since it is ugly and big. root = tk.Tk() root.withdraw() # Make it almost invisible - no decorations, 0 size, top left corner. root.overrideredirect(True) root.geometry('0x0+0+0') # Show window again and lift it to top so it can get focus, # otherwise dialogs will end up behind the terminal. root.deiconify() root.lift() root.focus_force() filenames = filedialog.askopenfilenames(parent=root) # Or some other dialog # Get rid of the top-level instance once to make it actually invisible. root.destroy() |