在 python 中 pygtk.main() 之后继续执行代码
我有一个应用程序,其中我的 DataFetch() 类“包裹”我的 HBHTray() 类,以便与该类的函数/变量进行交互。不幸的是,在我的 DataFetch() 类创建 HBHTray 实例并调用它之后,我似乎无法让代码继续执行,并且在 HBHTray 的 Start() 方法上它挂起。
这是相关代码:
class DataFetch(): # I need the DataFetch class to be wrapping around HBHTray so I can call/edit certain variables from within functions in DataFetch
def init(self):
self.Interval, self.Username, self.Password, self.CheckShoutBox = GetOptions('.tray')
self.Gui = HBHTray()
print '1'
self.Gui.Start()
print '2'
def Login(self):
pass # Do stuff (Eg: Edit self.Gui.StatusIcon state or call self.Gui.Notify()
def Start(self):
print 'hi!'
sleep(self.Interval)
print 'Hi!'
class HBHTray():
def init(self):
self.StatusIcon = gtk.StatusIcon()
self.StatusIcon.set_from_file('img')
self.StatusIcon.set_tooltip('No new messages')
self.Menu = gtk.Menu()
self.CheckBox = gtk.CheckMenuItem('Notify')
self.CheckBox.connect('activate', self.ChangeCheckBox)
self.CheckBox.set_active(True)
self.Menu.append(self.CheckBox)
self.MenuItem = gtk.ImageMenuItem('Options')
self.MenuItem.connect('activate', self.Options, self.StatusIcon)
self.Menu.append(self.MenuItem)
self.MenuItem = gtk.ImageMenuItem('About')
self.MenuItem.connect('activate', self.About, self.StatusIcon)
self.Menu.append(self.MenuItem)
self.MenuItem = gtk.ImageMenuItem('Quit')
self.MenuItem.connect('activate', self.Quit, self.StatusIcon)
self.Menu.append(self.MenuItem)
self.StatusIcon.connect('popup-menu', self.PopMenu, self.Menu)
self.StatusIcon.set_visible(1)
def PopMenu(self, widget, button, time, data = None):
if data:
data.show_all()
data.popup(None, None, gtk.status_icon_position_menu, 3, time, self.StatusIcon)
def Notify(self, message):
pynotify.init('null')
notification = pynotify.Notification('Something', message, 'dialogue')
notification.attach_to_status_icon(self.StatusIcon)
notification.show()
def Start(self):
gtk.main()
def About(self, a, b):
self.Notify('test')
def Options(self, a, b):
print a, b
def ChangeCheckBox(self, null):
pass
def Quit(self, a, b):
raise SystemExit
if name == 'main':
try:
gobject.threads_init() # Doesn't work?
Monitor = DataFetch()
Monitor.Start()
抱歉,糟糕的格式,Stack Overflow 似乎不喜欢空行...... 无论如何,虽然打印了“1”,但没有打印“2”。所以,gtk.main() 显然是它挂在的地方。有什么方法可以让我继续执行并让 gtk 去做它自己的事情吗?
I have an application where my DataFetch() class "Wraps" around my HBHTray() class for the purpose of interacting with the functions/variables of that class. Unfortunately, I can't seem to be able to get the code to continue execution after my DataFetch() class makes a instance of HBHTray and calls it, and on the Start() method of HBHTray it hangs.
This is the relevant code:
class DataFetch(): # I need the DataFetch class to be wrapping around HBHTray so I can call/edit certain variables from within functions in DataFetch
def init(self):
self.Interval, self.Username, self.Password, self.CheckShoutBox = GetOptions('.tray')
self.Gui = HBHTray()
print '1'
self.Gui.Start()
print '2'
def Login(self):
pass # Do stuff (Eg: Edit self.Gui.StatusIcon state or call self.Gui.Notify()
def Start(self):
print 'hi!'
sleep(self.Interval)
print 'Hi!'
class HBHTray():
def init(self):
self.StatusIcon = gtk.StatusIcon()
self.StatusIcon.set_from_file('img')
self.StatusIcon.set_tooltip('No new messages')
self.Menu = gtk.Menu()
self.CheckBox = gtk.CheckMenuItem('Notify')
self.CheckBox.connect('activate', self.ChangeCheckBox)
self.CheckBox.set_active(True)
self.Menu.append(self.CheckBox)
self.MenuItem = gtk.ImageMenuItem('Options')
self.MenuItem.connect('activate', self.Options, self.StatusIcon)
self.Menu.append(self.MenuItem)
self.MenuItem = gtk.ImageMenuItem('About')
self.MenuItem.connect('activate', self.About, self.StatusIcon)
self.Menu.append(self.MenuItem)
self.MenuItem = gtk.ImageMenuItem('Quit')
self.MenuItem.connect('activate', self.Quit, self.StatusIcon)
self.Menu.append(self.MenuItem)
self.StatusIcon.connect('popup-menu', self.PopMenu, self.Menu)
self.StatusIcon.set_visible(1)
def PopMenu(self, widget, button, time, data = None):
if data:
data.show_all()
data.popup(None, None, gtk.status_icon_position_menu, 3, time, self.StatusIcon)
def Notify(self, message):
pynotify.init('null')
notification = pynotify.Notification('Something', message, 'dialogue')
notification.attach_to_status_icon(self.StatusIcon)
notification.show()
def Start(self):
gtk.main()
def About(self, a, b):
self.Notify('test')
def Options(self, a, b):
print a, b
def ChangeCheckBox(self, null):
pass
def Quit(self, a, b):
raise SystemExit
if name == 'main':
try:
gobject.threads_init() # Doesn't work?
Monitor = DataFetch()
Monitor.Start()
Sorry for the terrible formatting, Stack Overflow doesn't seem to like blank lines....
Anyways, though, "1" is printed, but "2" is not. So, gtk.main() is obviously where it's hanging. Is there any way to allow me to continue execution and have gtk go do it's own thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
gobject.threads_init()
不会神奇地将你的东西放入单独的线程中。它只告诉库您将使用线程,并设置一些锁定。您仍然需要自己创建线程。gobject.threads_init()
does not magically put your things into separate threads. It only tells the library that you're going to use threads, and sets up some locking. You'll still have to create the threads yourself.事实证明,问题是我找不到有效的解决方案,因为我以错误的方式使用 Thread 模块,并在应该调用 start() 时直接调用 run() 。因此,我认为我所做的一切都不起作用(特别是没有任何错误或抱怨),并认为这是与我如何包装 gtk.main() 相关的问题
Turns out the problem was that I couldn't find a working solution because I was utilizing the Thread module the wrong way and calling run() directly when I should have been calling start(). Because of that, I was thinking that nothing I did worked (especially with no error or complaint from anything) and figured it was a problem relating to how I was wrapping gtk.main()