System der verwendete nicht angezeigt: Windows 7 Python: 2.7.12 Glade: 3.14.2 Bibliothek verwendet: matplotlib, NetworkXMatplotlib auf Glad/GTK3 scrolledwindow Einbettung nicht maplotlib
Hallo, ich anzuzeigen versuchen Einbettung von Matplotlibs Figur auf GTK. Die Anzeige im gescrollten Fenster funktioniert nicht. Wenn ich jedoch versuchte, es mit der Symbolleiste zu speichern, kann ich es tatsächlich speichern.
Der Code ist unten:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from figure1 import figure
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
class main_function():
def __init__(self):
interface= Gtk.Builder()
interface.add_from_file("interface1.glade")
interface.connect_signals(self)
# relier les signaux aux fonctions
# on stocke notre fichier Glade dans des variables
self.mainWindow=interface.get_object("mainWindow")
self.aboutchronomap=interface.get_object("aboutchronomap")
self.fichierdialogue=interface.get_object("fichierdialogue")
self.sw=interface.get_object("mainFigure")
self.sw2=interface.get_object("MatplotlibToolbar")
# on stocke les widgets avec les interfaces
canvas = FigureCanvas(figure())
print(type(canvas))
print(type(figure()))
self.sw.add_with_viewport(canvas)
print("self.sw.add_with_viewport(canvas)")
toolbar = NavigationToolbar(canvas, self.mainWindow)
self.sw2.add_with_viewport(toolbar)
print("toolbar display")
main_function()
Gtk.main()
figure1.py Code:
from gi.repository import Gtk
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from numpy import sin, cos, pi, linspace
def figure():
fig = Figure(figsize=(5,5), dpi=80)
ax = fig.add_subplot(111)
n = 1000
xsin = linspace(-pi, pi, n, endpoint=True)
xcos = linspace(-pi, pi, n, endpoint=True)
ysin = sin(xsin)
ycos = cos(xcos)
sinwave = ax.plot(xsin, ysin, color='black', label='sin(x)')
coswave = ax.plot(xcos, ycos, color='black', label='cos(x)', linestyle='--')
ax.set_xlim(-pi,pi)
ax.set_ylim(-1.2,1.2)
ax.fill_between(xsin, 0, ysin, (ysin - 1) > -1, color='blue', alpha=.3)
ax.fill_between(xsin, 0, ysin, (ysin - 1) < -1, color='red', alpha=.3)
ax.fill_between(xcos, 0, ycos, (ycos - 1) > -1, color='blue', alpha=.3)
ax.fill_between(xcos, 0, ycos, (ycos - 1) < -1, color='red', alpha=.3)
ax.legend(loc='upper left')
ax = fig.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
return fig
Glade XML-Code von scrolledwindow, wo die Zahl sollte normalerweise angezeigt werden:
<child>
<object class="GtkScrolledWindow" id="mainFigure">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">always</property>
<property name="vscrollbar_policy">always</property>
<property name="shadow_type">in</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
, was ich habe als Anzeige:
Wenn ich auf die Schaltfläche „Speichern“ in der Symbolleiste verwenden, ich habe tatsächlich die richtige Figur:
Auf der GTK, kann ich es nicht sehen. Kann mir jemand helfen?
Vielen Dank! Mia
Schließlich gelang es mir, die Lösung zu finden, sollten Sie nur hinzufügen „self.mainWindow.show_all()“ und es funktioniert. –