总的来说,我对 Tkinter 和 GUI 非常陌生,但我尝试四处寻找类似的问题(并发现了一些),但似乎仍然无法使其正常工作。我想在单击按钮后保存字符串路径“path_to_ifc_file”,以便稍后在另一个脚本中使用它。
from tkinter import *
from tkinter import filedialog
def BrowseFiles():
path_to_ifc_file = filedialog.askopenfilename(initialdir="../models/", title="Get Ifc-File:",
filetypes=(("Ifc-Files", "*.ifc*"), ("All files", "*.*")))
if path_to_ifc_file == "":
label_file_explorer.configure(text="Get Ifc-File:")
else:
label_file_explorer.configure(text="Found Ifc-File: \n" + Path(path_to_ifc_file).stem + ".ifc")
return path_to_ifc_file
# WINDOW CONFIGURATION
window = Tk()
window.title('Test')
window.geometry("800x280")
# INITIALIZE WIDGETS
label_file_explorer = Label(window, text="Get Ifc-File:")
button_explore = Button(window, text="Browse", command=BrowseFiles)
# DISPLAY WIDGETS
label_file_explorer.grid(column=1, row=1)
button_explore.grid(column=2, row=1)
window.mainloop()
在我看来,我只想要这样的东西
path_to_ifc_file=<returned value path_to_ifc_file from function BrowseFiles()>
我感谢我能得到的每一个帮助!提前致谢:)