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

Skip to content

Cleanup GTK examples. #12984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 14 additions & 19 deletions examples/user_interfaces/gtk_spreadsheet_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
48 changes: 22 additions & 26 deletions examples/user_interfaces/pylab_with_gtk_sgskip.py
Original file line number Diff line number Diff line change
@@ -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('<span color="#ef0000">x,y=(%f, %f)</span>' % (event.xdata, event.ydata))
label.set_markup(
f'<span color="#ef0000">x,y=({event.xdata}, {event.ydata})</span>')

plt.connect('motion_notify_event', update)
fig.canvas.mpl_connect('motion_notify_event', update)

plt.show()