激活 Tkinter Toplevel [英] Make a Tkinter Toplevel active
问题描述
我正在尝试制作一个 Toplevel
小部件,它是屏幕上的活动窗口(我想要它,以便如果您按 Enter,它会退出窗口.我已经有键绑定到小部件,但我似乎无法让窗口成为我计算机上的主窗口.我正在使用 Notepad++ 运行我的程序(我有这个特定程序的快捷方式,因为我将使用它很多).
I am trying to make a Toplevel
widget that is the active window on the screen (I want it so that if you press Enter, it exits the window. I already have the key bound to the widget, but I can't seem to get the window to be the main window on my computer. I am running my program using Notepad++ (I have a shortcut for this specific program since I will be using it a lot).
这是我的代码:
def main():
root = Tk(className = ' Module Opener')
app = GetFileName(root)
root.rowconfigure(0, weight = 1)
root.columnconfigure(0, weight = 1)
root.bind('<Return>', (lambda e, b=app.goButton: b.invoke()))
root.mainloop()
f, pythonType = app.fileName, app.pythonType
if f[-3:] != '.py': f += '.py'
moduleFile = getFilePath(f, pythonType)
if not moduleFile is None:
subprocess.call([r"C:\Program Files\Notepad++\notepad++.exe", moduleFile])
else:
root.withdraw()
finalRoot = Toplevel(root)
finalRoot.grab_set() # I thought this would make it active
finalApp = FileNotExist(finalRoot, f)
finalRoot.rowconfigure(0, weight = 1)
finalRoot.columnconfigure(0, weight = 1)
finalRoot.bind('<Return>', (lambda e, b=finalApp.okButton: b.invoke()))
finalRoot.mainloop()
我想要它,当它打开时,如果我按 Enter,它会执行我的命令;但是,我必须先在窗口中单击以使其处于活动状态,然后才能工作.
I want it so that when it opens, if I press Enter, it does my command; however, I have to click in the window first so that it becomes active, and then it works.
我尝试了各种方法,例如 finalRoot.lift()
、finalRoot.focus_set()
、finalRoot.grab_set()
/finalRoot.grab_set_global()
(我从 另一个问题) 和 finalRoot.focus()
.
I tried various things such as finalRoot.lift()
, finalRoot.focus_set()
, finalRoot.grab_set()
/finalRoot.grab_set_global()
(I saw these methods from another question), and finalRoot.focus()
.
第一个窗口 Tk()
在程序启动时处于活动状态.但是,Toplevel()
不是.我还尝试制作两个 Tk()
(销毁 root
然后创建 finalRoot
作为新的 Tk()
> 实例),但这并不奏效.我怎样才能做到这一点?谢谢!
The first window Tk()
is active when the program starts. However, the Toplevel()
is not. I also tried making two Tk()
's (destroying root
and then creating finalRoot
as a new Tk()
instance), but this did not work as well. How can I do this? Thanks!
推荐答案
...但是,我必须先在窗口中单击以使其处于活动状态,然后才能工作.
...however, I have to click in the window first so that it becomes active, and then it works.
我刚遇到这个问题,在研究解决方案时,我发现了这个线程.我正在使用 Windows 7 专业版.我所做的只是调用 both grab_set()
和 focus()
,它为我解决了这个问题.你已经有了finalRoot.grab_set()
,只需添加:
I just encountered this problem and while I was researching a solution, I found this thread. I'm using Windows 7 Professional. All I did was call both grab_set()
and focus()
and it solved the problem for me. You already have finalRoot.grab_set()
, just add:
finalRoot.focus()
它在我的情况下有效.
这篇关于激活 Tkinter Toplevel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!