Tkinter main window focus

后端 未结 6 654
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 10:36

I have the following code

window = Tk()
window.lift()
window.attributes(\"-topmost\", True)

This co

6条回答
  •  粉色の甜心
    2020-12-09 11:11

    Not sure what issue you face. But below is sample code which worked great for me.

    from tkinter import *
    
    window = Tk()
    
    def handle_focus(event):
        if event.widget == window:
            window.focus_set()
            input1.focus_set()
    
    
    label1 = Label(window,text = "Enter Text 2")
    input1 = Entry(window, bd=5)
    
    label2 = Label(window,text = "Enter Text 2")
    input2 = Entry(window, bd=5)
    
    submit = Button(window, text="Submit")
    
    label1.pack()
    input1.pack()
    label2.pack()
    input2.pack()
    submit.pack(side=BOTTOM)
    
    window.lift()
    window.attributes("-topmost", True)
    
    window.bind("", handle_focus)
    
    hwnd = window.winfo_id()
    
    window.mainloop()
    

    This was tested using Latest Python 3.6 on Windows 10.

    The result was that after running the program, I could just start typing and and he input would go to the first text box

提交回复
热议问题