From fe36a2550a585da586b44ef55ca7591a2ac7859e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 13 Dec 2018 14:10:13 +0100 Subject: [PATCH] Cleanup GTK examples. Right now they emit warnings: ``` gtk_spreadsheet_sgskip.py:36: PyGTKDeprecationWarning: Using positional arguments with the GObject constructor has been deprecated. Please specify keyword(s) for "homogeneous, spacing" or use a class specific constructor. See: https://wiki.gnome.org/PyGObject/InitializerDeprecations vbox = Gtk.VBox(False, 8) gtk_spreadsheet_sgskip.py:39: PyGTKDeprecationWarning: Using positional arguments with the GObject constructor has been deprecated. Please specify keyword(s) for "label" or use a class specific constructor. See: https://wiki.gnome.org/PyGObject/InitializerDeprecations label = Gtk.Label('Double click a row to plot the data') gtk_spreadsheet_sgskip.py:51: PyGTKDeprecationWarning: Using positional arguments with the GObject constructor has been deprecated. Please specify keyword(s) for "model" or use a class specific constructor. See: https://wiki.gnome.org/PyGObject/InitializerDeprecations self.treeview = Gtk.TreeView(model) gtk_spreadsheet_sgskip.py:52: DeprecationWarning: Gtk.TreeView.set_rules_hint is deprecated self.treeview.set_rules_hint(True) ``` ``` pylab_with_gtk_sgskip.py:30: PyGTKDeprecationWarning: Using positional arguments with the GObject constructor has been deprecated. Please specify keyword(s) for "label" or use a class specific constructor. See: https://wiki.gnome.org/PyGObject/InitializerDeprecations button = Gtk.Button('Click me') ``` Fix that, and some more. --- .flake8 | 2 +- .../user_interfaces/gtk_spreadsheet_sgskip.py | 33 ++++++------- .../user_interfaces/pylab_with_gtk_sgskip.py | 48 +++++++++---------- 3 files changed, 37 insertions(+), 46 deletions(-) diff --git a/.flake8 b/.flake8 index 76c3063f41c2..4cb738b4b544 100644 --- a/.flake8 +++ b/.flake8 @@ -252,7 +252,7 @@ per-file-ignores = examples/user_interfaces/gtk_spreadsheet_sgskip.py: E402 examples/user_interfaces/mathtext_wx_sgskip.py: E402, E501 examples/user_interfaces/mpl_with_glade3_sgskip.py: E402 - examples/user_interfaces/pylab_with_gtk_sgskip.py: E402, E501 + examples/user_interfaces/pylab_with_gtk_sgskip.py: E302, E402 examples/user_interfaces/toolmanager_sgskip.py: E402 examples/userdemo/custom_boxstyle01.py: E402 examples/userdemo/pgf_preamble_sgskip.py: E402 diff --git a/examples/user_interfaces/gtk_spreadsheet_sgskip.py b/examples/user_interfaces/gtk_spreadsheet_sgskip.py index 476022db1c44..bbeb2b2c7e99 100644 --- a/examples/user_interfaces/gtk_spreadsheet_sgskip.py +++ b/examples/user_interfaces/gtk_spreadsheet_sgskip.py @@ -3,55 +3,51 @@ GTK Spreadsheet =============== -Example of embedding matplotlib in an application and interacting with -a treeview to store data. Double click on an entry to update plot -data - +Example of embedding Matplotlib in an application and interacting with a +treeview to store data. Double click on an entry to update plot data. """ + import gi gi.require_version('Gtk', '3.0') gi.require_version('Gdk', '3.0') from gi.repository import Gtk, Gdk -from matplotlib.backends.backend_gtk3agg import FigureCanvas -# from matplotlib.backends.backend_gtk3cairo import FigureCanvas +from matplotlib.backends.backend_gtk3agg import FigureCanvas # or gtk3cairo. from numpy.random import random from matplotlib.figure import Figure class DataManager(Gtk.Window): - numRows, numCols = 20, 10 + num_rows, num_cols = 20, 10 - data = random((numRows, numCols)) + data = random((num_rows, num_cols)) def __init__(self): - Gtk.Window.__init__(self) + super().__init__() self.set_default_size(600, 600) self.connect('destroy', lambda win: Gtk.main_quit()) self.set_title('GtkListStore demo') self.set_border_width(8) - vbox = Gtk.VBox(False, 8) + vbox = Gtk.VBox(homogeneous=False, spacing=8) self.add(vbox) - label = Gtk.Label('Double click a row to plot the data') + label = Gtk.Label(label='Double click a row to plot the data') vbox.pack_start(label, False, False, 0) sw = Gtk.ScrolledWindow() sw.set_shadow_type(Gtk.ShadowType.ETCHED_IN) - sw.set_policy(Gtk.PolicyType.NEVER, - Gtk.PolicyType.AUTOMATIC) + sw.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) vbox.pack_start(sw, True, True, 0) model = self.create_model() - self.treeview = Gtk.TreeView(model) - self.treeview.set_rules_hint(True) + self.treeview = Gtk.TreeView(model=model) - # matplotlib stuff + # Matplotlib stuff fig = Figure(figsize=(6, 4)) self.canvas = FigureCanvas(fig) # a Gtk.DrawingArea @@ -75,14 +71,13 @@ def plot_row(self, treeview, path, view_column): self.canvas.draw() def add_columns(self): - for i in range(self.numCols): + for i in range(self.num_cols): column = Gtk.TreeViewColumn(str(i), Gtk.CellRendererText(), text=i) self.treeview.append_column(column) def create_model(self): - types = [float]*self.numCols + types = [float] * self.num_cols store = Gtk.ListStore(*types) - for row in self.data: store.append(tuple(row)) return store diff --git a/examples/user_interfaces/pylab_with_gtk_sgskip.py b/examples/user_interfaces/pylab_with_gtk_sgskip.py index 093105f1bd46..82cb3d3e82b0 100644 --- a/examples/user_interfaces/pylab_with_gtk_sgskip.py +++ b/examples/user_interfaces/pylab_with_gtk_sgskip.py @@ -1,62 +1,58 @@ """ =============== -Pyplot With GTK +pyplot with GTK =============== -An example of how to use pyplot to manage your figure windows, but -modify the GUI by accessing the underlying gtk widgets +An example of how to use pyplot to manage your figure windows, but modify the +GUI by accessing the underlying GTK widgets. """ + import matplotlib matplotlib.use('GTK3Agg') # or 'GTK3Cairo' import matplotlib.pyplot as plt +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk -fig, ax = plt.subplots() -plt.plot([1, 2, 3], 'ro-', label='easy as 1 2 3') -plt.plot([1, 4, 9], 'gs--', label='easy as 1 2 3 squared') -plt.legend() +fig, ax = plt.subplots() +ax.plot([1, 2, 3], 'ro-', label='easy as 1 2 3') +ax.plot([1, 4, 9], 'gs--', label='easy as 1 2 3 squared') +ax.legend() -manager = plt.get_current_fig_manager() -# you can also access the window or vbox attributes this way +manager = fig.canvas.manager +# you can access the window or vbox attributes this way toolbar = manager.toolbar +vbox = manager.vbox # now let's add a button to the toolbar -import gi -gi.require_version('Gtk', '3.0') -from gi.repository import Gtk -pos = 8 # where to insert this in the mpl toolbar -button = Gtk.Button('Click me') +button = Gtk.Button(label='Click me') button.show() - - -def clicked(button): - print('hi mom') -button.connect('clicked', clicked) +button.connect('clicked', lambda button: print('hi mom')) toolitem = Gtk.ToolItem() toolitem.show() toolitem.set_tooltip_text('Click me for fun and profit') - toolitem.add(button) + +pos = 8 # where to insert this in the mpl toolbar toolbar.insert(toolitem, pos) -pos += 1 # now let's add a widget to the vbox label = Gtk.Label() label.set_markup('Drag mouse over axes for position') label.show() -vbox = manager.vbox vbox.pack_start(label, False, False, 0) -vbox.reorder_child(manager.toolbar, -1) - +vbox.reorder_child(toolbar, -1) def update(event): if event.xdata is None: label.set_markup('Drag mouse over axes for position') else: - label.set_markup('x,y=(%f, %f)' % (event.xdata, event.ydata)) + label.set_markup( + f'x,y=({event.xdata}, {event.ydata})') -plt.connect('motion_notify_event', update) +fig.canvas.mpl_connect('motion_notify_event', update) plt.show()