【问题标题】:minimize to tray, stuck in loop, python gtk最小化到托盘,卡在循环中,python gtk
【发布时间】:2014-11-08 16:39:35
【问题描述】:

蟒蛇:2.7.5

操作系统:linux、fedora 19

这是显示我遇到的问题的最小示例代码,代码的托盘部分被排除在外,因为它工作正常。

在终端中运行它,当按下最小化按钮时,它会进入show()hide() 循环

我认为显示它的最简单方法是让它在每次显示或隐藏时打印一个数字。

问题是:如何让它正常工作而不会陷入循环?

from gi.repository import Gtk, Gdk

class Win(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("delete-event", self.delete_event)
        #something to make it easier to see the loop
        self.num = 0
        self.connect('window-state-event', self.window_state_event_cb)

    def window_state_event_cb(self, widget, event):
       if event.changed_mask & Gdk.WindowState.ICONIFIED:
          if event.new_window_state & Gdk.WindowState.ICONIFIED:
             self.hide()
             print 'Window was minimized!'
          else:
             self.show()
             print 'Window was unminimized!'
          self.num += 1
          print(self.num)

    def delete_event(self,window,event):
        Gtk.main_quit()

if __name__=="__main__":
    win = Win()
    win.show_all()
    Gtk.main()

http://faq.pygtk.org/index.py?req=edit&file=faq10.022.htp拼凑的代码

【问题讨论】:

  • 在哪种情况下会陷入循环?
  • when the minimize button is press
  • 我尝试了您使用 pygtk-2.24.2 发布的代码,它可以最小化而不会卡住。尝试删除/禁用所有其他事件处理程序并测试您发布的代码是否真的自行循环。
  • 它在 linux 上循环,可能是 linux 的问题,但我对此表示怀疑,你是在终端/命令行中运行的吗?
  • Yes 命令行为python filename.py,我在其中粘贴了您的代码并为我的 pygtk 版本调整了导入等。可能是 linux(我正在赢),可能是你的 gtk 版本...

标签: python linux pygobject


【解决方案1】:

我有同样的问题,这对我有用。

def on_windowStateEvent(self, widget, event):
  if (event.changed_mask & Gdk.WindowState.ICONIFIED):
    if (event.new_window_state & Gdk.WindowState.ICONIFIED):
      # minimize visible widow. We hide the window to remove it
      # from the active tasks windows, a click the tray icon will
      # bring it back
      self.window.hide()
    else:
      # don't do anything here. The call from hide() above causes a 
      # new event that ends here. And calling show() now will result
      # in a loop
      pass

def statusIcon_activate(self, statusIcon):
  # restore the hidden window on a click on the tray icon
  self.window.deiconify()
  self.window.present() # present the window on the active desktop

...
self.window = Gtk.Window()
self.window.connect("window-state-event", self.on_windowStateEvent)
....
self.statusIcon = Gtk.StatusIcon()
self.statusIcon.connect("activate", self.statusIcon_activate)
...

还有一个简写形式:

def isEventIconify(event):
  return (event.changed_mask
          & event.new_window_state
          & Gdk.WindowState.ICONIFIED)

def on_windowStateEvent(self, widget, event):
  if isEventIconify(event):
    self.window.hide()

【讨论】:

  • 嵌套的if 是多余的。你只需要and 条件一起。考虑到您已经在玩一些小游戏,您也许可以将 & 全部放在一起(event.changed_maskevent.new_window_stateGdk.WindowState.ICONIFIED),但我不是 100% 确定。
  • 是的,但这是一个非常冗长的示例,基于上面的代码和 PyGTK FAQ 中的代码参考。它仍然保留了添加更多代码的地方,因为它希望使“最小化到托盘”功能可配置。
猜你喜欢
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
  • 2013-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多