2011-01-27 54 views
0

如果我第二次調用這個類,鍵盤焦點首次進入窗口。第二次沒有。退出鍵用於關閉窗口,它首次運行。第二次必須在退出鍵工作之前用鼠標點擊窗口。有沒有傳遞的焦點沒有被傳回?我遇到過這個問題很多次,但在這裏我儘可能地簡單了。下面的代碼:第二次對Python應用程序的調用怎麼沒有鍵盤焦點

from Tkinter import * 

class Dialog(): # imagine this having buttons and stuff 
    def __init__(self): 
     root = Tk() 
     root.bind_all('<Escape>', lambda event :root.destroy()) 
     root.mainloop() 

# this simulates running a python terminal app which invokes a dialog at some point 
Dialog() # this one accepts the key action 
Dialog() # this one needs a mouse click in the window first 
+1

但是,爲什麼你會退出應用程序只是爲了重新啓動它? – 2011-01-27 11:19:09

回答

0

我剛剛運行的代碼,並在Windows 7中,它並不需要關注兩種時間。

你在使用什麼操作系統?

此外,您可以通過投入此行強制焦點到根窗口。

root.focus_force()

0

在Mac上使用X11作爲窗口服務器,並使用或者X終端或常規終端,焦點總是轉到對話框窗口,以便逃生關鍵是雙向倍。在從cmd.exe終端運行的Windows XP中,焦點僅在第一次進入對話框。第二次,您必須先點擊窗口。但是,使用root.focus_force()可以在任何地方使用它。謝謝一堆!

相關問題