2012-01-26 10 views
3

아래의 MailThread.run()에 지정된 스레드에서 Gtk 상태 아이콘의 상태를 변경하려고하지만 상태 아이콘 객체에 도달하는 방법을 모르겠습니다. 메서드에서 set_visible을 True 또는 False로 변경합니다.Python에서 전역 변수에 액세스하기

기본적으로 "# 설정 상태 아이콘 보이기/켜기"대신 쓸 내용을 알고 싶습니다.

#!/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() 

답변

4

당신의 상태 아이콘을 받아 들일 수 스레드의 __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는 기본적으로 "큐에 저를 추가하고 약간의 자유 시간이있을 때 그것을"를 의미한다.

+0

감사합니다. – Max

+0

@Max : 문제가 없으며 스택 오버플로에 오신 것을 환영합니다! 내 대답으로 문제가 해결되면 옆에있는 체크 표시를 클릭하여 문제를 수락 할 수 있습니다. – cha0site

+0

감사합니다, cha0site. 체크 표시를 찾았습니다. 문제가 해결되었지만 아이콘이 표시되지 않도록 변경하면 이상한 "BadIDChoice"오류가 표시됩니다. 다른 방법은 잘 작동합니다. – Max

관련 문제