python - 出现文件选择对话框时,Tkinter GUI 消失
问题描述
背景:我正在为 PDF 应用程序构建 GUI。此应用程序要求用户选择文件的位置并提供一些附加信息,以便可以生成封面然后附加到 PDF。我选择了 Tkinter 来为这个应用程序创建 GUI。我在 Mac OS 上开发。
问题:我可以在按下表单的按钮时生成一个文件选择对话框,但是,在文件选择对话框出现后,GUI 立即消失。有人知道是什么原因造成的吗?
from tkinter import *
from tkinter import filedialog
root = Tk()
Label(root, text='Submittal No. ').grid(row=0)
Label(root, text='Project Name ').grid(row=1)
Label(root, text='Product Name ').grid(row=2)
e1 = Entry(root)
e2 = Entry(root)
e3 = Entry(root)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
def fileSelector():
root.withdraw()
root.fileName = filedialog.askopenfilename()
print(root.fileName)
Button(root, text='Select PDF file ', command=fileSelector).grid(row=3)
if __name__ == "__main__":
mainloop()
解决方案
在您的 fileSelector 函数中,行 root.withdraw 用于使根窗口消失,同时使其保持活动状态。
您可以删除该行以使 GUI 保持可见,或者如果您想阻止用户与 GUI 交互,直到解决了 filedialog.askopenfilename,您可以稍后使用 deiconify 函数重新出现该窗口:
def fileSelector():
global filename
root.withdraw()
root.fileName = filedialog.askopenfilename()
root.deiconify()
print(root.fileName)
推荐阅读
- python - 如何根据另一个数据框的值返回列中的值
- ios - 罕见的使用 Apple 登录崩溃
- postgresql - 调试高 PostgreSQL 内存使用率(每个连接)
- paypal - paypal onboard before payment 403 Forbidden 错误,问题:MISSING_SCOPES
- javascript - 覆盖默认 TabIndex 行为以首先选择选中的元素
- python - 如何在 pywebhfds 中通过连接错误?
- android - android kotlin 数据绑定 kapt 丢失
- macos - 无法验证您的申请。我们无法创建身份验证会话
- javascript - 通过 FTP 到 JSON 的不完整 CSV
- r - 检索一组具有唯一子字符串的字符串