2012-07-14 68 views
2

我使用pygtk創建了一個新窗口。我希望系統立即給它鍵盤焦點。我所做的大部分時間都在工作,但是當窗口已經有焦點時,我的新窗口將被忽略。有沒有辦法強制我的窗口進入鍵盤焦點?我用來打開窗口的代碼是:Force grab keyboard focus pygtk

self.window = gtk.Window() 
    self.window.set_position(gtk.WIN_POS_CENTER) 
    self.window.connect("key-press-event", self.keypress) 
    self.window.connect("focus-out-event", self.cancel) 
    self.window.connect("destroy", self.cancel) 
    self.entry = gtk.Entry(200) 
    button = gtk.Button("go") 
    button.connect("clicked", self.command) 
    box = gtk.HBox() 
    box.add(self.entry) 
    box.add(button) 
    self.window.add(box) 
    self.window.set_keep_above(True) 
    self.window.show_all() 
    self.window.window.focus() 

回答

2

進行焦點呼叫時窗口不可見。試試這個:

def create_window(self): 
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
    self.window.set_position(gtk.WIN_POS_CENTER) 
    self.window.connect("key-press-event", self.keypress) 
    self.window.connect("focus-out-event", self.cancel) 
    self.window.connect("destroy", self.cancel) 
    self.entry = gtk.Entry(200) 
    button = gtk.Button("go") 
    button.connect("clicked", self.command) 
    box = gtk.HBox() 
    box.add(self.entry) 
    box.add(button) 
    self.window.add(box) 
    self.window.set_keep_above(True) 
    self.window.show_all() 
    gtk.idle_add(self.bring_to_front) 

def bring_to_front(self): 
    self.window.present()