Skip to content

Commit 56947bd

Browse files
committed
DOC: Convert remaining GTK2 examples to GTK3.
1 parent 40e3879 commit 56947bd

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

examples/user_interfaces/gtk_spreadsheet_sgskip.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,54 @@
88
data
99
1010
"""
11-
import pygtk
12-
pygtk.require('2.0')
13-
import gtk
14-
from gtk import gdk
11+
import gi
12+
gi.require_version('Gtk', '3.0')
13+
gi.require_version('Gdk', '3.0')
14+
from gi.repository import Gtk, Gdk
1515

16-
import matplotlib
17-
matplotlib.use('GTKAgg') # or 'GTK'
18-
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
16+
from matplotlib.backends.backend_gtk3agg import FigureCanvas
17+
# from matplotlib.backends.backend_gtk3cairo import FigureCanvas
1918

2019
from numpy.random import random
2120
from matplotlib.figure import Figure
2221

2322

24-
class DataManager(gtk.Window):
23+
class DataManager(Gtk.Window):
2524
numRows, numCols = 20, 10
2625

2726
data = random((numRows, numCols))
2827

2928
def __init__(self):
30-
gtk.Window.__init__(self)
29+
Gtk.Window.__init__(self)
3130
self.set_default_size(600, 600)
32-
self.connect('destroy', lambda win: gtk.main_quit())
31+
self.connect('destroy', lambda win: Gtk.main_quit())
3332

3433
self.set_title('GtkListStore demo')
3534
self.set_border_width(8)
3635

37-
vbox = gtk.VBox(False, 8)
36+
vbox = Gtk.VBox(False, 8)
3837
self.add(vbox)
3938

40-
label = gtk.Label('Double click a row to plot the data')
39+
label = Gtk.Label('Double click a row to plot the data')
4140

42-
vbox.pack_start(label, False, False)
41+
vbox.pack_start(label, False, False, 0)
4342

44-
sw = gtk.ScrolledWindow()
45-
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
46-
sw.set_policy(gtk.POLICY_NEVER,
47-
gtk.POLICY_AUTOMATIC)
48-
vbox.pack_start(sw, True, True)
43+
sw = Gtk.ScrolledWindow()
44+
sw.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
45+
sw.set_policy(Gtk.PolicyType.NEVER,
46+
Gtk.PolicyType.AUTOMATIC)
47+
vbox.pack_start(sw, True, True, 0)
4948

5049
model = self.create_model()
5150

52-
self.treeview = gtk.TreeView(model)
51+
self.treeview = Gtk.TreeView(model)
5352
self.treeview.set_rules_hint(True)
5453

5554
# matplotlib stuff
5655
fig = Figure(figsize=(6, 4))
5756

58-
self.canvas = FigureCanvas(fig) # a gtk.DrawingArea
59-
vbox.pack_start(self.canvas, True, True)
57+
self.canvas = FigureCanvas(fig) # a Gtk.DrawingArea
58+
vbox.pack_start(self.canvas, True, True, 0)
6059
ax = fig.add_subplot(111)
6160
self.line, = ax.plot(self.data[0, :], 'go') # plot the first row
6261

@@ -65,9 +64,9 @@ def __init__(self):
6564

6665
self.add_columns()
6766

68-
self.add_events(gdk.BUTTON_PRESS_MASK |
69-
gdk.KEY_PRESS_MASK |
70-
gdk.KEY_RELEASE_MASK)
67+
self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK |
68+
Gdk.EventMask.KEY_PRESS_MASK |
69+
Gdk.EventMask.KEY_RELEASE_MASK)
7170

7271
def plot_row(self, treeview, path, view_column):
7372
ind, = path # get the index into data
@@ -77,18 +76,18 @@ def plot_row(self, treeview, path, view_column):
7776

7877
def add_columns(self):
7978
for i in range(self.numCols):
80-
column = gtk.TreeViewColumn('%d' % i, gtk.CellRendererText(), text=i)
79+
column = Gtk.TreeViewColumn(str(i), Gtk.CellRendererText(), text=i)
8180
self.treeview.append_column(column)
8281

8382
def create_model(self):
8483
types = [float]*self.numCols
85-
store = gtk.ListStore(*types)
84+
store = Gtk.ListStore(*types)
8685

8786
for row in self.data:
88-
store.append(row)
87+
store.append(tuple(row))
8988
return store
9089

9190

9291
manager = DataManager()
9392
manager.show_all()
94-
gtk.main()
93+
Gtk.main()

examples/user_interfaces/pylab_with_gtk_sgskip.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
modify the GUI by accessing the underlying gtk widgets
88
"""
99
import matplotlib
10-
matplotlib.use('GTKAgg')
10+
matplotlib.use('GTK3Agg') # or 'GTK3Cairo'
1111
import matplotlib.pyplot as plt
1212

1313

@@ -22,32 +22,32 @@
2222
toolbar = manager.toolbar
2323

2424
# now let's add a button to the toolbar
25-
import gtk
25+
import gi
26+
gi.require_version('Gtk', '3.0')
27+
from gi.repository import Gtk
2628
next = 8 # where to insert this in the mpl toolbar
27-
button = gtk.Button('Click me')
29+
button = Gtk.Button('Click me')
2830
button.show()
2931

3032

3133
def clicked(button):
3234
print('hi mom')
3335
button.connect('clicked', clicked)
3436

35-
toolitem = gtk.ToolItem()
37+
toolitem = Gtk.ToolItem()
3638
toolitem.show()
37-
toolitem.set_tooltip(
38-
toolbar.tooltips,
39-
'Click me for fun and profit')
39+
toolitem.set_tooltip_text('Click me for fun and profit')
4040

4141
toolitem.add(button)
4242
toolbar.insert(toolitem, next)
4343
next += 1
4444

4545
# now let's add a widget to the vbox
46-
label = gtk.Label()
46+
label = Gtk.Label()
4747
label.set_markup('Drag mouse over axes for position')
4848
label.show()
4949
vbox = manager.vbox
50-
vbox.pack_start(label, False, False)
50+
vbox.pack_start(label, False, False, 0)
5151
vbox.reorder_child(manager.toolbar, -1)
5252

5353

0 commit comments

Comments
 (0)