为什么 wxframe 不是从使用全局 gtk 绑定器调用的函数中引发的?
好吧,为什么这个简单的应用程序不起作用。 我花了一天时间调查这个问题,但一无所获。
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()
我不知道为什么,但有时(特别是当我通过在面板上单击应用程序来提升应用程序时)提升不起作用,并且我得到了闪存图标而不是凸起的窗口。
更新
好吧,我回到主题并注意到这些..
- 上面的例子对我有用..奇怪
我隔离了下面代码显示的奇怪行为..它与wnck lib有关。因此,如果我的应用程序窗口通过打开新窗口(左键单击窗口 - test1)停用,则 raise 效果完美,但如果使用 wnck(右键单击 - test2)激活其他窗口(将“opera”替换为您的任何窗口),则激活失败
导入日志记录 导入子流程 导入时间 导入wnck 导入wx
logging.basicConfig(level=logging.DEBUG)
类 MyFrame(wx.Frame):
def __init__(self, 父级, title=''): wx.Frame.__init__(self, 父级, title=title) self.Center() self.Bind(wx.EVT_LEFT_DOWN, self.test1) self.Bind(wx.EVT_RIGHT_DOWN, self.raise_window) def test1(自身,evt): logging.debug('丢失..') subprocess.Popen(['xterm']) 时间.睡眠(1) self.Raise() 日志记录.debug('丢失') def loss_focus_by_wnck(self): 屏幕 = wnck.screen_get_default() 导入gtk 而 gtk.events_pending(): gtk.main_iteration(False) 胜利 = screen.get_windows() logging.debug('胜利: {0}'.format(wins)) 为了赢得胜利: app_name = win.get_application().get_name() logging.debug('应用程序:{0}'.format(app_name)) 如果 app_name.lower() 中为“opera”: win_id = win.get_xid() 休息 别的: win_id = 无 返回win_id def test2(自身,evt): logging.debug('丢失..') win_id = self.lose_focus_by_wnck() win = wnck.window_get(win_id) 时间戳 = 0 win.activate(时间戳) 日志记录.debug('丢失') 时间.睡眠(1) self.Raise() 日志记录.debug('引发')
如果名称 == '主要': 应用程序 = wx.PySimpleApp(重定向=False) 框架 = MyFrame(无) 框架.Show() app.MainLoop()
有没有人理解这种行为,而不是像我感觉的那样非常有帮助? :)
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..
- above example works for me.. strange
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 wxlogging.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? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
什么是按键绑定器?您使用 AcceleratorTable 吗?请参阅 http://www.blog.pythonlibrary.org /2010/12/02/wxpython-keyboard-shortcuts-accelerators/ 了解更多信息。我认为你不能将 pyGtk 与 wxPython 混合使用。
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.