我试图运行这个函数,只要条目是1或2,但是在第一个有效输入(1或2)之后,第二个输入什么也不做,似乎被循环卡住了。
(函数打印选定的文件名+输入号)
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如何更改代码以便多次使用tkinter文件浏览器(askopenfilename()函数)?
发布于 2021-11-28 09:43:47
您可以简单地创建和销毁根窗口来实现这一点,但这并不是一种高性能的方法。
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您可以修改这段代码,使文件在屏幕的前面和中间。我还为透明添加了alpha,您甚至注意到了发生了什么:
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) https://stackoverflow.com/questions/70141950
复制相似问题