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).
Here is my code:
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 FilesNotepad++
otepad++.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()
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.
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()
.
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!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…