Форум сайта python.su
Доброго времени суток
Подскажите пожалуйста..
#!/usr/bin/python import os #import signal import json from urllib2 import Request, urlopen from gi.repository import GObject as gobject from gi.repository import Gtk as gtk from gi.repository import AppIndicator3 as appindicator from gi.repository import Notify as notify class IndicatorFly: def __init__(self): self.indicator = appindicator.Indicator.new(APPINDICATOR_ID, os.path.abspath('icon.svg'), appindicator.IndicatorCategory.SYSTEM_SERVICES) self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE) notify.init(APPINDICATOR_ID) #Main Menu self.main_menu = gtk.Menu() self.listMenu = gtk.Menu() # Sub menu self.listItems = gtk.MenuItem("Configuration") self.listItems.set_submenu(self.listMenu) # Joke in sub Menu self._joke = gtk.MenuItem("Joke") self.listMenu.append(self._joke) self._joke.connect('activate', self.joke) #Config in sub menu self._config = gtk.MenuItem("Config") self.listMenu.append(self._config) #Tool tips self._config.set_tooltip_text(" Test ") self._config.connect('activate', self.win) #Notify in sub menu self._notify = gtk.CheckMenuItem("Notify") self._notify.connect('activate', self.status) #self._notify.set_active(True) # create exit menu self._quit = gtk.MenuItem("Quit") self._quit.connect("activate", self.quit) self.main_menu.append(self.listItems) self.main_menu.append(self._notify) self.main_menu.append(self._quit) self.main_menu.show_all() self.indicator.set_menu(self.main_menu) def onButtonPressed(self, widget): self.label.set_text("Hello World") def win(self, widget): builder = gtk.Builder() builder.add_from_file("main.glade") window = builder.get_object("main_window") label = builder.get_object("label1") builder.connect_signals(IndicatorFly()) window.show_all() #gtk.main() # Procedure for exit def quit(self, widget): notify.uninit() gtk.main_quit() def fetch_joke(self, widget): request = Request('http://api.icndb.com/jokes/random?limitTo=[nerdy]') response = urlopen(request) joke = json.loads(response.read())['value']['joke'] return joke def joke(self): #print "Joke "+str(show_notify) if show_notify: notify.Notification.new("<b>Joke</b>", self.fetch_joke(self), None).show() return True def status(self,widget): global show_notify if widget.get_active(): show_notify = True else: show_notify = False if __name__ == "__main__": APPINDICATOR_ID = "Myapp" show_notify= False #gtk.timeout_add(3000, indicator.joke) indicator = IndicatorFly() gobject.timeout_add(3000, indicator.joke) gtk.main()
Traceback (most recent call last): File "/home/flyer/Cloud/Work/Programming/Python/Applet/applet2.py", line 59, in onButtonPressed self.label.set_text("Hello World") AttributeError: IndicatorFly instance has no attribute 'label'
Офлайн
main.glade - file
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated with glade 3.18.3 --> <interface> <requires lib="gtk+" version="3.0"/> <object class="GtkWindow" id="main_window"> <property name="can_focus">False</property> <property name="window_position">center-always</property> <child> <object class="GtkBox" id="box1"> <property name="visible">True</property> <property name="can_focus">False</property> <property name="orientation">vertical</property> <child> <object class="GtkLabel" id="label1"> <property name="visible">True</property> <property name="can_focus">False</property> </object> <packing> <property name="expand">True</property> <property name="fill">False</property> <property name="padding">83</property> <property name="position">0</property> </packing> </child> <child> <object class="GtkButton" id="button1"> <property name="label" translatable="yes">button</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="halign">center</property> <property name="valign">center</property> <property name="margin_left">50</property> <property name="margin_right">50</property> <property name="margin_bottom">10</property> <signal name="pressed" handler="onButtonPressed" swapped="no"/> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">1</property> </packing> </child> <child> <placeholder/> </child> </object> </child> </object> </interface>
Офлайн
flyerЗначит, при вызове передаётся аргумент, тогда как .joke() не ожидает аргументов.TypeError: joke() takes exactly 1 argument (2 given)
flyerhttp://zetcode.com/gui/pygtk/signals/self._joke.connect('activate', self.joke)
Офлайн
py.user.nextЕсли делаю def joke(self, widget):
http://zetcode.com/gui/pygtk/signals/
У .joke() должен быть аргумент widget.
TypeError: joke() takes exactly 2 arguments (1 given)
Офлайн
Есдинсвенное, я не знаю костыль или нетб спасает без ошибок
def joke(self, widget=None):
Отредактировано flyer (Окт. 4, 2017 17:47:59)
Офлайн
flyergobject.timeout_add(3000, indicator.joke)
gobject.timeout_add(3000, indicator.joke, None)
Отредактировано py.user.next (Окт. 4, 2017 18:02:44)
Офлайн
Да, все сработало… спасибо огромное за помощь!
Офлайн