Merge lp:~oly/python-snippets/interactive-python-textview into lp:python-snippets

Proposed by Oliver Marks
Status: Needs review
Proposed branch: lp:~oly/python-snippets/interactive-python-textview
Merge into: lp:python-snippets
Diff against target: 100 lines (+84/-0)
2 files modified
CATEGORIES (+1/-0)
gtk3/interactive-python-textview.py (+83/-0)
To merge this branch: bzr merge lp:~oly/python-snippets/interactive-python-textview
Reviewer Review Type Date Requested Status
Akkana Peck Pending
Review via email: mp+191117@code.launchpad.net

Description of the change

gtk3 example of using a textview to run interactive python to modify the running application.

To post a comment you must log in.

Unmerged revisions

101. By Oliver Marks

example of writing python inside an existing gtk application using a textview.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CATEGORIES'
2--- CATEGORIES 2010-04-24 18:34:32 +0000
3+++ CATEGORIES 2013-10-15 07:57:54 +0000
4@@ -32,6 +32,7 @@
5 os os Python module OS related snippets.
6 OpenOffice.org Snippets the handle OpenOffice.org files.
7 PyGTK PyGTK widget and framework examples.
8+ GTK3 GTK 3 widgets using gi repository library
9 PyGTKSourceView PyGTKSourceView widget and framework examples.
10 Python Core Examples that demonstrate core features in the Python language.
11 Python VTE Snippets using the VTE widget for GTK.
12
13=== added directory 'gtk3'
14=== added file 'gtk3/interactive-python-textview.py'
15--- gtk3/interactive-python-textview.py 1970-01-01 00:00:00 +0000
16+++ gtk3/interactive-python-textview.py 2013-10-15 07:57:54 +0000
17@@ -0,0 +1,83 @@
18+#!/usr/bin/env python
19+# [SNIPPET_NAME: gtk3 interactive textview]
20+# [SNIPPET_CATEGORIES: gtk3]
21+# [SNIPPET_TAGS:interactive, gtk3]
22+# [SNIPPET_DESCRIPTION: using gtk3 textview run python code on running program]
23+# [SNIPPET_AUTHOR: Oliver Marks ]
24+# [SNIPPET_LICENSE: GPL]
25+
26+from gi.repository import Gtk, Gdk
27+import code
28+import math
29+
30+
31+class interactiveGtk:
32+ def __init__(self):
33+ window = Gtk.Window()
34+ window.set_default_size(380, 300)
35+ window.connect("destroy", lambda w: Gtk.main_quit())
36+
37+ box = Gtk.VBox()
38+
39+ self.drawingarea = Gtk.DrawingArea()
40+
41+ textarea = Gtk.TextView()
42+ textarea.connect('key-press-event', self.key_pressed)
43+ self.textbuffer = textarea.get_buffer()
44+ self.textbuffer.set_text('self.show_drawing(True)')
45+
46+ self.drawingarea.connect("draw", self.area_expose_cb)
47+
48+ box.add(self.drawingarea)
49+ box.add(textarea)
50+
51+ window.add(box)
52+ window.show_all()
53+
54+ self.drawarea = False
55+
56+ """interactive mode interpreter, pass locals() so we can access our programs functions and variables"""
57+ self.interpreter = code.InteractiveInterpreter(locals())
58+
59+ def show_drawing(self, state):
60+ """self.show_drawing(True) to enable showing the lines"""
61+ self.drawarea = state
62+
63+ def test(self):
64+ """run self.test() when program is running to print this message"""
65+ print 'hello world'
66+
67+ def area_expose_cb(self, widget, context):
68+ """expose event lets draw, lines will only display if we run self.show_lines first.
69+ demonstrating running state change of our program"""
70+ self.style = self.drawingarea.get_style()
71+ #self.gc = self.style.fg_gc[Gtk.STATE_NORMAL]
72+ if self.drawarea is True:
73+ self.drawing(context, 210, 10)
74+
75+ def drawing(self, cr, x, y):
76+ """ draw a circle in the drawing area """
77+ cr.set_line_width(10)
78+ cr.set_source_rgb(0.5, 0.8, 0.0)
79+
80+ cr.translate(20 / 2, 20 / 2)
81+ cr.arc(50, 50, 50, 0, 2 * math.pi)
82+ cr.stroke_preserve()
83+
84+ cr.set_source_rgb(0.3, 0.4, 0.4)
85+ cr.fill()
86+
87+ def key_pressed(self, widget, event):
88+ """keypresses only intrested in return, run code in textview on return"""
89+ if event.keyval == Gdk.keyval_from_name('Return'):
90+ start = self.textbuffer.get_iter_at_line(0)
91+ lineend = start.get_chars_in_line()
92+ end = self.textbuffer.get_end_iter()
93+ source = self.textbuffer.get_text(start, end, False)
94+
95+ """run our code in the textview widget, put output in the terminal we may want to put this into the gui somewhere"""
96+ print self.interpreter.runsource(source, "<<console>>")
97+
98+
99+interactiveGtk()
100+Gtk.main()

Subscribers

People subscribed via source and target branches

to all changes: