Why wxframe isn't raised from a function called with global gtk binder?

好久不见. 提交于 2019-12-25 03:53:45

问题


Ok, why this simple app dosn't work. I've spent one day investigating this and got nothing.

import wx, os
import gtk
import keybinder

class FrameWithHotKey(wx.Frame):

    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        keybinder.bind("<Ctrl>period", self.toggle_shown)

    def toggle_shown(self):
        # windowNow id
        if self.IsShown():
            self.Hide()
        else:
            self.Show()
            self.Raise()


if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = FrameWithHotKey(None)
    app.MainLoop()

I don't know why, but sometimes (especially when I raise apps by clicking on them on panel) raising dosen't work and I got flash icon instead of raised window.

UPDATE

Ok, i return to the topic and notice these..

  1. above example works for me.. strange
  2. i isolated strange behaviour which below code shows.. it's something related with wnck lib. So if my app window is deactivated by open new window (left click on window - test1) then raise works perfect, but if other window (replace 'opera' with any of Yours) is activated with wnck(by right click - test2) then actvation fails

    import logging import subprocess import time import wnck import wx

    logging.basicConfig(level=logging.DEBUG)

    class MyFrame(wx.Frame):

    def __init__(self, parent, title=''):
        wx.Frame.__init__(self, parent, title=title)
        self.Centre()
        self.Bind(wx.EVT_LEFT_DOWN, self.test1)
        self.Bind(wx.EVT_RIGHT_DOWN, self.raise_window)
    
    def test1(self, evt):
        logging.debug('losing..')
        subprocess.Popen(['xterm'])
        time.sleep(1)
        self.Raise()
        logging.debug('lost')
    
    def lose_focus_by_wnck(self):
        screen = wnck.screen_get_default()
    
        import gtk
        while gtk.events_pending():
            gtk.main_iteration(False)
    
        wins = screen.get_windows()
        logging.debug('wins: {0}'.format(wins))
        for win in  wins:
            app_name = win.get_application().get_name()
            logging.debug('app: {0}'.format(app_name))
            if 'opera' in app_name.lower():
                win_id = win.get_xid()
                break
        else:
            win_id = None
        return win_id
    
    def test2(self, evt):
        logging.debug('losing..')
        win_id = self.lose_focus_by_wnck()
        win = wnck.window_get(win_id)
        TIMESTAMP = 0
        win.activate(TIMESTAMP)
        logging.debug('lost')
        time.sleep(1)
        self.Raise()
        logging.debug('raised')
    

    if name == 'main': app = wx.PySimpleApp(redirect=False) frame = MyFrame(None) frame.Show() app.MainLoop()

Does anybody understand this behaviour instead of very helpful wtf like i feel? :)


回答1:


What is keybinder? Are you using an AcceleratorTable? See http://www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/ for more info. I don't think you can mix pyGtk with wxPython.



来源:https://stackoverflow.com/questions/6398417/why-wxframe-isnt-raised-from-a-function-called-with-global-gtk-binder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!