[英]TopLevel window disappears when using askopenfilename, from tkinter.filedialog
For my program I want the user to select a file
, and I am using the tkinter.filedialog
library to do this. 对于我的程序,我希望用户选择一个
file
,并且我正在使用tkinter.filedialog
库来执行此操作。 However, when the askopenfilename
dialog is opened, the TopLevel
window disappears behind the main Tk()
window. 但是,当打开
askopenfilename
对话框时, TopLevel
窗口消失在主Tk()
窗口后面。
How would I stop this from happening?
我将如何阻止这种情况的发生?
Here is the code that I have written so far: 这是我到目前为止编写的代码:
from tkinter import *
from tkinter.filedialog import askopenfilename
class MainWin(Tk):
def __init__(self):
super(MainWin, self).__init__()
self.update()
pu = PopUp(self)
self.configure(width=500, height=300)
class PopUp(Toplevel):
def __init__(self, master):
super(PopUp, self).__init__(master)
def entry_set(entry, text):
entry.delete(0, 'end')
entry.insert(END, text)
item_file = StringVar()
item_entry = Entry(self, textvariable=item_file)
item_entry.place(x=80, y=60, height=20, width=300)
item_label = Label(self, text="item file: ", bg="gray74", relief="groove")
item_label.place(x=20, y=60, height=20, width=60)
item_button = Button(self, text="\uD83D\uDCC2", relief="groove",
command=lambda: entry_set(item_entry, askopenfilename()))
item_button.place(x=380, y=60, height=20, width=20)
self.configure(width=460, height=180)
if __name__ == '__main__':
win = MainWin()
win.mainloop()
Edit:
编辑:
I have realised that using the
.grab_set()
method works, and will make theTopLevel()
window appear back on top of theTk()
after the file is chosen.我已经意识到使用
.grab_set()
方法是.grab_set()
,并且在选择文件后,将使TopLevel()
窗口重新显示在Tk()
顶部。
However, this still means the window disappears behind the Tk()
window whilst picking a file, I would still love to find a solution to this, even though this is now just a visual problem, not a functional one. 但是,这仍然意味着该窗口在选择文件时消失在
Tk()
窗口后面,我仍然很乐于找到解决方案,即使现在这只是视觉问题,而不是功能问题。
You can just make the Toplevel
window a transient window, it will then be kept on top of its parent window: 你可以只让
Toplevel
窗口短暂的窗口,然后将它保存在它的父窗口的顶部:
class PopUp(Toplevel):
def __init__(self, master):
super(PopUp, self).__init__(master)
self.transient(master)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.