Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 952f8b6

Browse files
authored
Merge pull request #12984 from anntzer/gtk-examples
Cleanup GTK examples.
2 parents 20bdd96 + fe36a25 commit 952f8b6

File tree

3 files changed

+37
-46
lines changed

3 files changed

+37
-46
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ per-file-ignores =
252252
examples/user_interfaces/gtk_spreadsheet_sgskip.py: E402
253253
examples/user_interfaces/mathtext_wx_sgskip.py: E402, E501
254254
examples/user_interfaces/mpl_with_glade3_sgskip.py: E402
255-
examples/user_interfaces/pylab_with_gtk_sgskip.py: E402, E501
255+
examples/user_interfaces/pylab_with_gtk_sgskip.py: E302, E402
256256
examples/user_interfaces/toolmanager_sgskip.py: E402
257257
examples/userdemo/custom_boxstyle01.py: E402
258258
examples/userdemo/pgf_preamble_sgskip.py: E402

examples/user_interfaces/gtk_spreadsheet_sgskip.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,51 @@
33
GTK Spreadsheet
44
===============
55
6-
Example of embedding matplotlib in an application and interacting with
7-
a treeview to store data. Double click on an entry to update plot
8-
data
9-
6+
Example of embedding Matplotlib in an application and interacting with a
7+
treeview to store data. Double click on an entry to update plot data.
108
"""
9+
1110
import gi
1211
gi.require_version('Gtk', '3.0')
1312
gi.require_version('Gdk', '3.0')
1413
from gi.repository import Gtk, Gdk
1514

16-
from matplotlib.backends.backend_gtk3agg import FigureCanvas
17-
# from matplotlib.backends.backend_gtk3cairo import FigureCanvas
15+
from matplotlib.backends.backend_gtk3agg import FigureCanvas # or gtk3cairo.
1816

1917
from numpy.random import random
2018
from matplotlib.figure import Figure
2119

2220

2321
class DataManager(Gtk.Window):
24-
numRows, numCols = 20, 10
22+
num_rows, num_cols = 20, 10
2523

26-
data = random((numRows, numCols))
24+
data = random((num_rows, num_cols))
2725

2826
def __init__(self):
29-
Gtk.Window.__init__(self)
27+
super().__init__()
3028
self.set_default_size(600, 600)
3129
self.connect('destroy', lambda win: Gtk.main_quit())
3230

3331
self.set_title('GtkListStore demo')
3432
self.set_border_width(8)
3533

36-
vbox = Gtk.VBox(False, 8)
34+
vbox = Gtk.VBox(homogeneous=False, spacing=8)
3735
self.add(vbox)
3836

39-
label = Gtk.Label('Double click a row to plot the data')
37+
label = Gtk.Label(label='Double click a row to plot the data')
4038

4139
vbox.pack_start(label, False, False, 0)
4240

4341
sw = Gtk.ScrolledWindow()
4442
sw.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
45-
sw.set_policy(Gtk.PolicyType.NEVER,
46-
Gtk.PolicyType.AUTOMATIC)
43+
sw.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
4744
vbox.pack_start(sw, True, True, 0)
4845

4946
model = self.create_model()
5047

51-
self.treeview = Gtk.TreeView(model)
52-
self.treeview.set_rules_hint(True)
48+
self.treeview = Gtk.TreeView(model=model)
5349

54-
# matplotlib stuff
50+
# Matplotlib stuff
5551
fig = Figure(figsize=(6, 4))
5652

5753
self.canvas = FigureCanvas(fig) # a Gtk.DrawingArea
@@ -75,14 +71,13 @@ def plot_row(self, treeview, path, view_column):
7571
self.canvas.draw()
7672

7773
def add_columns(self):
78-
for i in range(self.numCols):
74+
for i in range(self.num_cols):
7975
column = Gtk.TreeViewColumn(str(i), Gtk.CellRendererText(), text=i)
8076
self.treeview.append_column(column)
8177

8278
def create_model(self):
83-
types = [float]*self.numCols
79+
types = [float] * self.num_cols
8480
store = Gtk.ListStore(*types)
85-
8681
for row in self.data:
8782
store.append(tuple(row))
8883
return store
Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,58 @@
11
"""
22
===============
3-
Pyplot With GTK
3+
pyplot with GTK
44
===============
55
6-
An example of how to use pyplot to manage your figure windows, but
7-
modify the GUI by accessing the underlying gtk widgets
6+
An example of how to use pyplot to manage your figure windows, but modify the
7+
GUI by accessing the underlying GTK widgets.
88
"""
9+
910
import matplotlib
1011
matplotlib.use('GTK3Agg') # or 'GTK3Cairo'
1112
import matplotlib.pyplot as plt
1213

14+
import gi
15+
gi.require_version('Gtk', '3.0')
16+
from gi.repository import Gtk
1317

14-
fig, ax = plt.subplots()
15-
plt.plot([1, 2, 3], 'ro-', label='easy as 1 2 3')
16-
plt.plot([1, 4, 9], 'gs--', label='easy as 1 2 3 squared')
17-
plt.legend()
1818

19+
fig, ax = plt.subplots()
20+
ax.plot([1, 2, 3], 'ro-', label='easy as 1 2 3')
21+
ax.plot([1, 4, 9], 'gs--', label='easy as 1 2 3 squared')
22+
ax.legend()
1923

20-
manager = plt.get_current_fig_manager()
21-
# you can also access the window or vbox attributes this way
24+
manager = fig.canvas.manager
25+
# you can access the window or vbox attributes this way
2226
toolbar = manager.toolbar
27+
vbox = manager.vbox
2328

2429
# now let's add a button to the toolbar
25-
import gi
26-
gi.require_version('Gtk', '3.0')
27-
from gi.repository import Gtk
28-
pos = 8 # where to insert this in the mpl toolbar
29-
button = Gtk.Button('Click me')
30+
button = Gtk.Button(label='Click me')
3031
button.show()
31-
32-
33-
def clicked(button):
34-
print('hi mom')
35-
button.connect('clicked', clicked)
32+
button.connect('clicked', lambda button: print('hi mom'))
3633

3734
toolitem = Gtk.ToolItem()
3835
toolitem.show()
3936
toolitem.set_tooltip_text('Click me for fun and profit')
40-
4137
toolitem.add(button)
38+
39+
pos = 8 # where to insert this in the mpl toolbar
4240
toolbar.insert(toolitem, pos)
43-
pos += 1
4441

4542
# now let's add a widget to the vbox
4643
label = Gtk.Label()
4744
label.set_markup('Drag mouse over axes for position')
4845
label.show()
49-
vbox = manager.vbox
5046
vbox.pack_start(label, False, False, 0)
51-
vbox.reorder_child(manager.toolbar, -1)
52-
47+
vbox.reorder_child(toolbar, -1)
5348

5449
def update(event):
5550
if event.xdata is None:
5651
label.set_markup('Drag mouse over axes for position')
5752
else:
58-
label.set_markup('<span color="#ef0000">x,y=(%f, %f)</span>' % (event.xdata, event.ydata))
53+
label.set_markup(
54+
f'<span color="#ef0000">x,y=({event.xdata}, {event.ydata})</span>')
5955

60-
plt.connect('motion_notify_event', update)
56+
fig.canvas.mpl_connect('motion_notify_event', update)
6157

6258
plt.show()

0 commit comments

Comments
 (0)