Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 104 lines (72 sloc) 2.734 kB
a7a32a2 @tkf Add examples/gtk
authored
1 #!/usr/bin/env python
2
32878ba @tkf Document examples/gtk/server.py bit more
authored
3 """
4 An example of using GTK from Emacs.
5
6 * Manipulate GTK window from Emacs:
7
a5cf498 @tkf Change docstring in examples/gtk/server.py
authored
8 Python server exposes remote methods called `destroy` and
9 `set_button_label`. You can call these methods from Emacs by
10 ``M-x pyepc-sample-gtk-destroy`` and
11 ``M-x pyepc-sample-gtk-set-button-label``.
12
32878ba @tkf Document examples/gtk/server.py bit more
authored
13 - `destroy` (`pyepc-sample-gtk-destroy`):
14 Close GTK window from Emacs command
15
16 - `set_button_label` (`pyepc-sample-gtk-set-button-label`):
17 Change GUI button label from Emacs command.
18
7aec3eb @tkf Add Python to Emacs communication example in examples/gtk
authored
19 * Manipulate Emacs from GTK GUI.
20
a5cf498 @tkf Change docstring in examples/gtk/server.py
authored
21 Emacs client exposes a remote method called `message`.
22
23 - `message`:
24 Emacs prints message in minibuffer when GUI button is clicked.
32878ba @tkf Document examples/gtk/server.py bit more
authored
25
26 """
27
a7a32a2 @tkf Add examples/gtk
authored
28 import threading
29 import logging
30
31 import pygtk
32 pygtk.require('2.0')
33 import gtk
34 import gobject
35
8bd400f @tkf Do not use setuplogfile in examples/gtk/server.py
authored
36 from epc.server import ThreadingEPCServer
a7a32a2 @tkf Add examples/gtk
authored
37
38
39 class SampleGTKServer(object):
40
41 def __init__(self):
42 self.setup_gtk()
43 self.setup_epc()
44
45 def setup_gtk(self):
46 gtk.threads_init()
47
48 # Create a new window
49 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
50
51 # Quit when window is closed
52 self.window.connect("destroy", self.destroy)
53
a5cf498 @tkf Change docstring in examples/gtk/server.py
authored
54 # Creates a new button
a7a32a2 @tkf Add examples/gtk
authored
55 self.button = gtk.Button("Push me!")
56 self.button.connect("clicked", self.clicked, None)
57
58 # Show widgets and window
59 self.window.add(self.button)
60 self.button.show()
61 self.window.show()
62
63 def setup_epc(self):
b99b82e @tkf Set log_traceback=True in examples
authored
64 self.server = ThreadingEPCServer(('localhost', 0), log_traceback=True)
32878ba @tkf Document examples/gtk/server.py bit more
authored
65
66 # Setup logger
a7a32a2 @tkf Add examples/gtk
authored
67 self.server.logger.setLevel(logging.DEBUG)
8bd400f @tkf Do not use setuplogfile in examples/gtk/server.py
authored
68 ch = logging.FileHandler(filename='python-epc.log', mode='w')
69 ch.setLevel(logging.DEBUG)
70 self.server.logger.addHandler(ch)
32878ba @tkf Document examples/gtk/server.py bit more
authored
71
72 # Setup server thread
a7a32a2 @tkf Add examples/gtk
authored
73 self.server_thread = threading.Thread(target=self.server.serve_forever)
74 self.server_thread.allow_reuse_address = True
75
76 # Define and register RPC functions
77 def destroy():
78 gobject.idle_add(gtk.Widget.destroy, self.window)
79
80 def set_button_label(label):
81 gobject.idle_add(self.button.set_label, label)
82
83 self.server.register_function(destroy)
84 self.server.register_function(set_button_label)
85
86 def clicked(self, widget, data=None):
c0c2f21 @tkf Simplify SampleGTKServer
authored
87 handler = self.server.clients[0]
2eb0871 @tkf Tweak SampleGTKServer.clicked
authored
88 message = "Button '{0}' is clicked!".format(self.button.get_label())
89 handler.call('message', [message])
a7a32a2 @tkf Add examples/gtk
authored
90
91 def destroy(self, widget, data=None):
5729a29 @tkf Stop EPC server properly in pyepc-sample-gtk-destroy
authored
92 self.server.shutdown()
a7a32a2 @tkf Add examples/gtk
authored
93 gtk.main_quit()
94
95 def main(self):
96 self.server_thread.start()
97 self.server.print_port()
98 gtk.main()
99
100
101 if __name__ == "__main__":
102 server = SampleGTKServer()
103 server.main()
Something went wrong with that request. Please try again.