使用的系统:Windows 7 python:2.7.12 格莱德:3.14.2 使用的库:matplotlib,networkx
您好, 我试图在GTK上显示嵌入matplotlib的图。滚动窗口上的显示不起作用。但是当我尝试用工具栏保存它时,我实际上可以保存它。
代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_sign_in"
android:layout_width="match_parent"
android:background="@drawable/sign_up_bg"
android:layout_height="match_parent"
tools:showIn="@layout/activity_sign_in">
<ImageButton
android:id="@+id/arrow_back"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:scaleType="centerInside"
android:padding="5dp"
android:src="@drawable/ic_back_white"
android:background="@android:color/transparent"
android:layout_width="40dp"
android:layout_height="40dp" />
<ImageView
android:scaleType="centerInside"
android:layout_below="@+id/arrow_back"
android:id="@+id/logo"
android:layout_marginTop="6dp"
android:layout_centerHorizontal="true"
android:layout_width="78dp"
android:layout_height="45dp" />
<android.support.design.widget.TextInputLayout
android:textColorHint="@color/color_80ffffff"
android:id="@+id/input_layout_email"
android:layout_marginLeft="46dp"
android:hint=" "
android:layout_marginRight="46dp"
android:layout_marginTop="74dp"
android:layout_below="@+id/logo"
app:errorTextAppearance="@style/error_appearance"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/work_email"
android:gravity="center"
android:theme="@style/EditTextLogin"
android:textColorHint="@color/color_80ffffff"
android:hint="@string/hint_work_email"
android:textColor="@android:color/white"
android:textSize="14sp"
android:inputType="textEmailAddress"
android:imeOptions="actionNext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:textColorHint="@color/color_80ffffff"
android:id="@+id/input_layout_password"
android:layout_marginLeft="46dp"
android:hint=" "
android:theme="@style/EditTextLogin"
android:layout_marginRight="46dp"
android:layout_below="@+id/input_layout_email"
app:errorTextAppearance="@style/error_appearance"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/password"
android:gravity="center"
android:textColorHint="@color/color_80ffffff"
android:hint="@string/hint_password"
android:textColor="@android:color/white"
android:textSize="14sp"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<Button
android:layout_below="@+id/input_layout_password"
android:id="@+id/btn_sign_in"
android:textColor="@android:color/white"
android:text="@string/btn_login"
android:layout_marginTop="26dp"
android:background="@color/colorAccent"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"
android:layout_width="match_parent"
android:layout_height="40dp" />
<RelativeLayout
android:layout_marginTop="17dp"
android:layout_below="@+id/btn_sign_in"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/label_sign_up"
android:textSize="14sp"
android:text="@string/lbl_sign_up"
android:textColor="@color/color_80ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_toRightOf="@+id/label_sign_up"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:id="@+id/btn_sign_up"
android:textSize="14sp"
android:text="@string/txt_sign_up"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<TextView
android:id="@+id/forgot_password"
android:textColor="@color/color_80ffffff"
android:textSize="14sp"
android:gravity="center"
android:text="@string/text_forgot_password"
android:layout_marginBottom="20dp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:visibility="gone"
android:id="@+id/layout_resend"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="86dp">
<ImageView
android:scaleType="fitXY"
android:src="@drawable/pink_bar"
android:layout_width="match_parent"
android:layout_height="86dp" />
<Button
android:id="@+id/btn_resend"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="26dp"
android:textSize="14sp"
android:text="@string/txt_resend"
android:textColor="@color/color_505065"
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="40dp" />
<ImageView
android:id="@+id/ic_email"
android:layout_marginLeft="16dp"
android:layout_marginTop="26dp"
android:src="@drawable/ic_email_confirmation_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/ic_email"
android:layout_toLeftOf="@+id/btn_resend"
android:layout_marginTop="26dp"
android:textSize="14sp"
android:text="@string/text_resend_email"
android:textColor="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:visibility="gone"
android:id="@+id/load"
android:background="#aa000000"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_width="50dp"
android:layout_height="50dp" />
</RelativeLayout>
figure1.py代码:
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()
滚动窗口的Glade xml代码,通常显示图形:
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
我所拥有的显示内容:
在GTK上,我看不到它。 任何人都可以帮助我吗?
谢谢! 米娅
答案 0 :(得分:0)
我设法由我自己弄明白。你应该只添加self.mainWindow.show_all()并且这样可行。
此问题还有一些额外的资源。
为了将matplotlib图片嵌入GTK GUI,我使用下面的代码:
def on_carto_display_button_press_event(self,widget,event):
if self.View.display_Mode != "carto":
self.View.display_Mode = "carto"
child=self.View.sw.get_child()
child1 = self.View.toolbar.get_child()
#print(child1)
if child != None:
self.View.toolbar.remove(child1)
self.View.sw.remove(child)
self.box.remove(self.View.canvasChrono)
self.box=Gtk.Box()
self.View.sw.add(self.box)
self.box.pack_start(self.View.canvasCarto, True, True, 0)
#Add toolbar
toolbar = NavigationToolbar(self.View.canvasCarto, self.View.window)
self.View.toolbar.add_with_viewport(toolbar)
child1 = self.View.toolbar.get_child()
#print(child1)
self.View.sw.show_all()
#self.View.toolbar.show_all()
在GUi GTK中嵌入matplotlib图的另一个例子可以是以下一个:
#!/usr/bin/python3
from gi.repository import Gtk
from matplotlib.figure import Figure
from numpy import sin, cos, pi, linspace
#Possibly this rendering backend is broken currently
#from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
class Signals:
def on_window1_destroy(self, widget):
Gtk.main_quit()
builder = Gtk.Builder()
builder.add_objects_from_file('mpl-ntb-glade.glade', ('window1', '') )
builder.connect_signals(Signals())
myfirstwindow = builder.get_object('window1')
sw = builder.get_object('scrolledwindow1')
sw2 = builder.get_object('scrolledwindow2')
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))
fig.tight_layout()
canvas = FigureCanvas(fig)
sw.add_with_viewport(canvas)
toolbar = NavigationToolbar(canvas, myfirstwindow)
sw2.add_with_viewport(toolbar)
myfirstwindow.show_all()
Gtk.main()
&#13;
此文档对于帮助您解决问题非常有用=)