在Python中访问非全局变量(Accessing a non

编程入门 行业动态 更新时间:2024-10-14 04:24:55
在Python中访问非全局变量(Accessing a non-global variable in Python)

我正在尝试从下面的MailThread.run()中指定的线程更改Gtk状态图标的状态,但我不知道如何从该方法到达状态图标对象以便将set_visible更改为True或者错误。

基本上我想知道写什么来代替“#set status status icon visible / on”。

#!/usr/bin/env python import gtk, sys, pynotify, imaplib, time, threading from email import parser class Mail: def check_mail(self): obj = imaplib.IMAP4_SSL('imap.gmail.com','993') acc = 'email' pwrd = 'pass' obj.login(acc, pwrd) obj.select() num = str(len(obj.search(None,'UnSeen')[1][0].split())) return acc, num class MailThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) gtk.gdk.threads_init() def run(self): while True: print "hello" mail = Mail() num = mail.check_mail()[1] if num < 1: # set status icon visible off else: # set status icon visible on time.sleep(60) class StatusIcon: # activate callback def activate( self, widget, data=None): mail = Mail() acc, num = mail.check_mail() pynotify.init("myapp") n = pynotify.Notification(acc, "You have " + num + " unread e-mails.", "emblem-mail") n.show() # Show_Hide callback def show_hide(self, widget,response_id, data= None): if response_id == gtk.RESPONSE_YES: widget.hide() else: widget.hide() # destroyer callback def destroyer(self, widget,response_id, data= None): if response_id == gtk.RESPONSE_OK: gtk.main_quit() else: widget.hide() # popup callback def popup(self, button, widget, data=None): dialog = gtk.MessageDialog( parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT, type = gtk.MESSAGE_INFO, buttons = gtk.BUTTONS_OK_CANCEL, message_format = "Do you want to close e-mail notifications?") dialog.set_title('Exit') dialog.connect('response', self.destroyer) dialog.show() def __init__(self): # create a new Status Icon self.staticon = gtk.StatusIcon() self.staticon.set_from_icon_name("emblem-mail") self.staticon.connect("activate", self.activate) self.staticon.connect("popup_menu", self.popup) self.staticon.set_visible(True) # starting thread thread = MailThread() thread.setDaemon(True) thread.start() # invoking the main() gtk.main() if __name__ == "__main__": # status icon statusicon = StatusIcon()

I'm trying to change the state of a Gtk status icon from a thread as specified in MailThread.run() below, but I don't know how to reach the status icon object from the method in order to change set_visible to either True or False.

Basically I would like to know what to write in place of "# set status icon visible off/on".

#!/usr/bin/env python import gtk, sys, pynotify, imaplib, time, threading from email import parser class Mail: def check_mail(self): obj = imaplib.IMAP4_SSL('imap.gmail.com','993') acc = 'email' pwrd = 'pass' obj.login(acc, pwrd) obj.select() num = str(len(obj.search(None,'UnSeen')[1][0].split())) return acc, num class MailThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) gtk.gdk.threads_init() def run(self): while True: print "hello" mail = Mail() num = mail.check_mail()[1] if num < 1: # set status icon visible off else: # set status icon visible on time.sleep(60) class StatusIcon: # activate callback def activate( self, widget, data=None): mail = Mail() acc, num = mail.check_mail() pynotify.init("myapp") n = pynotify.Notification(acc, "You have " + num + " unread e-mails.", "emblem-mail") n.show() # Show_Hide callback def show_hide(self, widget,response_id, data= None): if response_id == gtk.RESPONSE_YES: widget.hide() else: widget.hide() # destroyer callback def destroyer(self, widget,response_id, data= None): if response_id == gtk.RESPONSE_OK: gtk.main_quit() else: widget.hide() # popup callback def popup(self, button, widget, data=None): dialog = gtk.MessageDialog( parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT, type = gtk.MESSAGE_INFO, buttons = gtk.BUTTONS_OK_CANCEL, message_format = "Do you want to close e-mail notifications?") dialog.set_title('Exit') dialog.connect('response', self.destroyer) dialog.show() def __init__(self): # create a new Status Icon self.staticon = gtk.StatusIcon() self.staticon.set_from_icon_name("emblem-mail") self.staticon.connect("activate", self.activate) self.staticon.connect("popup_menu", self.popup) self.staticon.set_visible(True) # starting thread thread = MailThread() thread.setDaemon(True) thread.start() # invoking the main() gtk.main() if __name__ == "__main__": # status icon statusicon = StatusIcon()

最满意答案

您可以接受线程的__init__()的状态图标:

class MailThread(threading.Thread): def __init__(self, status_icon = None): threading.Thread.__init__(self) gtk.gdk.threads_init() self.status_icon = status_icon

然后你可以在run()使用它。

此外,您需要从主线程执行所有GUI工作。 主线程有一个由GTK维护的队列,你可以用来告诉它去做一些GUI工作。 这是它的工作原理:

def run(self): # <...> if num < 1: gobject.idle_add(self.set_status_icon, False) else: gobject.idle_add(self.set_status_icon, True) # <...> def set_status_icon(self, state = False): # code that changes icon state goes here pass

idle_add基本上意味着“将其添加到队列中并在有空闲时间时执行”。

You can accept the status icon in the thread's __init__():

class MailThread(threading.Thread): def __init__(self, status_icon = None): threading.Thread.__init__(self) gtk.gdk.threads_init() self.status_icon = status_icon

And then you can use it in run().

Additionally, you need to do all the GUI work from the main thread. The main thread has a queue maintained by GTK you can use to tell it to go do some GUI work. This is how it works:

def run(self): # <...> if num < 1: gobject.idle_add(self.set_status_icon, False) else: gobject.idle_add(self.set_status_icon, True) # <...> def set_status_icon(self, state = False): # code that changes icon state goes here pass

idle_add basically means "add that to the queue and do it when you have some free time".

更多推荐

本文发布于:2023-08-03 22:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局变量   Python   Accessing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!