APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
 
import gnomeapplet
import gtk
import mosquitto
import sys
 
class CurrentCostMQTT(gnomeapplet.Applet):
    def on_message(self, mosq, obj, msg):
        # Message format is "power"
        self.label.set_text(msg.payload+"W")
 
    def set_label(self, val):
        self.label.set_text(val)
 
    def on_change_background(self, applet, type, color, pixmap):
        applet.set_style(None)
        applet.modify_style(gtk.RcStyle())
 
        if type == gnomeapplet.COLOR_BACKGROUND:
            applet.modify_bg(gtk.STATE_NORMAL, color)
        elif type == gnomeapplet.PIXMAP_BACKGROUND:
            style = applet.get_style().copy()
            style.bg_pixmap[gtk.STATE_NORMAL] = pixmap
            applet.set_style(style)
 
    def show_menu(self, widget, event):
        print "menu"
 
    def __init__(self, applet, iid):
        self.applet = applet
        self.label = gtk.Label("0W")
        self.event_box = gtk.EventBox()
        self.event_box.add(self.label)
        self.event_box.set_events(gtk.gdk.BUTTON_PRESS_MASK)
        self.event_box.connect("button_press_event", self.show_menu)
        self.applet.add(self.event_box)
        self.applet.set_background_widget(applet)
        self.applet.show_all()
        self.mosq = mosquitto.Mosquitto()
        self.mosq.on_message = self.on_message
        self.mosq.connect("localhost")
        self.mosq.loop_start()
        self.mosq.subscribe("sensors/cc128/ch1", 0)
        self.applet.connect('change-background', self.on_change_background)
 
def CurrentCostMQTT_factory(applet, iid):
    CurrentCostMQTT(applet, iid)
    return gtk.TRUE
 
if len(sys.argv) == 2:
    if sys.argv[1] == "-d": #Debug mode
        main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        main_window.set_title("Python Applet")
        main_window.connect("destroy", gtk.main_quit)
        app = gnomeapplet.Applet()
        CurrentCostMQTT_factory(app,None)
        app.reparent(main_window)
        main_window.show_all()
        gtk.main()
        sys.exit()
 
if __name__ == '__main__':
    gnomeapplet.bonobo_factory("OAFIID:CurrentCostMQTT_Factory", gnomeapplet.Applet.__gtype__, "MQTT", "0", CurrentCostMQTT_factory)