2015-06-09 109 views
1

嵌入Matplotlib地塊內GTK3我使用Python3Glade,我需要嵌入一個GtkBox動態改變內部的情節,我下面這個教程: http://gtk3-matplotlib-cookbook.readthedocs.org/en/latest/hello-plot.html#embedding-matplotlibPython3使用格萊德

只是似乎什麼都沒有

main.py:78:警告:發生了,我在執行過程中得到這個錯誤源ID 39沒有被發現當試圖刪除 Gtk.main()

這是關於劇情的部分代碼:

print("plot") 
figure = Figure(figsize=(5,5), dpi=100) 
ax = figure.add_subplot(111) 

N = 20 
theta = linspace(0.0, 2 * pi, N, endpoint=False) 
radii = 10 * random.rand(N) 
width = pi/4 * random.rand(N) 

bars = ax.bar(theta, radii, width=width, bottom=0.0) 

for r, bar in zip(radii, bars): 
    bar.set_facecolor(cm.jet(r/10.)) 
    bar.set_alpha(0.5) 
print("pre-plot") 
ax.plot() 
print("post-plot") 

canvas = FigureCanvas(figure) # a Gtk.DrawingArea 
containerbox.remove(schermata1) #schermata1= GTKBOX 
sw.add_with_viewport(canvas) #sw= GtkScrolledWindow 
containerbox.add(schermata2) #schermata2= GtkBox whit sw inside 

我進口:

from gi.repository import Gtk 
from numpy import arange, pi, random, linspace 

from matplotlib.figure import Figure 
from matplotlib.axes import Subplot 
from numpy import arange, sin, pi 
import matplotlib.cm as cm 
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas 

我空地文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Generated with glade 3.16.1 --> 
<interface> 
    <requires lib="gtk+" version="3.10"/> 
    <object class="GtkApplicationWindow" id="applicationwindow1"> 
    <property name="can_focus">False</property> 
    <child> 
     <object class="GtkBox" id="box1"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <property name="orientation">vertical</property> 
     <child> 
      <object class="GtkMenuBar" id="menubar1"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <child> 
       <object class="GtkMenuItem" id="menuitem1"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="label" translatable="yes">_File</property> 
       <property name="use_underline">True</property> 
       <child type="submenu"> 
        <object class="GtkMenu" id="menu1"> 
        <property name="visible">True</property> 
        <property name="can_focus">False</property> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem1"> 
         <property name="label">gtk-new</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem2"> 
         <property name="label">gtk-open</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem3"> 
         <property name="label">gtk-save</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem4"> 
         <property name="label">gtk-save-as</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkSeparatorMenuItem" id="separatormenuitem1"> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem5"> 
         <property name="label">gtk-quit</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        </object> 
       </child> 
       </object> 
      </child> 
      <child> 
       <object class="GtkMenuItem" id="menuitem2"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="label" translatable="yes">_Modifica</property> 
       <property name="use_underline">True</property> 
       <child type="submenu"> 
        <object class="GtkMenu" id="menu2"> 
        <property name="visible">True</property> 
        <property name="can_focus">False</property> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem6"> 
         <property name="label">gtk-cut</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem7"> 
         <property name="label">gtk-copy</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem8"> 
         <property name="label">gtk-paste</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem9"> 
         <property name="label">gtk-delete</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        </object> 
       </child> 
       </object> 
      </child> 
      <child> 
       <object class="GtkMenuItem" id="menuitem3"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="label" translatable="yes">_Visualizza</property> 
       <property name="use_underline">True</property> 
       </object> 
      </child> 
      <child> 
       <object class="GtkMenuItem" id="menuitem4"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="label" translatable="yes">A_iuto</property> 
       <property name="use_underline">True</property> 
       <child type="submenu"> 
        <object class="GtkMenu" id="menu3"> 
        <property name="visible">True</property> 
        <property name="can_focus">False</property> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem10"> 
         <property name="label">gtk-about</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        </object> 
       </child> 
       </object> 
      </child> 
      </object> 
      <packing> 
      <property name="expand">False</property> 
      <property name="fill">True</property> 
      <property name="position">0</property> 
      </packing> 
     </child> 
     <child> 
      <object class="GtkButtonBox" id="buttonbox1"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="homogeneous">True</property> 
      <property name="layout_style">spread</property> 
      <child> 
       <object class="GtkButton" id="Nuovo"> 
       <property name="label" translatable="yes">Nuovo</property> 
       <property name="visible">True</property> 
       <property name="can_focus">True</property> 
       <property name="receives_default">True</property> 
       <signal name="clicked" handler="nuovoevent" swapped="no"/> 
       </object> 
       <packing> 
       <property name="expand">False</property> 
       <property name="fill">True</property> 
       <property name="position">0</property> 
       </packing> 
      </child> 
      <child> 
       <object class="GtkButton" id="ok"> 
       <property name="label" translatable="yes">Ok</property> 
       <property name="visible">True</property> 
       <property name="can_focus">True</property> 
       <property name="receives_default">True</property> 
       <signal name="clicked" handler="okevent" swapped="no"/> 
       </object> 
       <packing> 
       <property name="expand">False</property> 
       <property name="fill">True</property> 
       <property name="position">1</property> 
       </packing> 
      </child> 
      </object> 
      <packing> 
      <property name="expand">False</property> 
      <property name="fill">True</property> 
      <property name="position">1</property> 
      </packing> 
     </child> 
     <child> 
      <object class="GtkBox" id="containerbox"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="orientation">vertical</property> 
      <child> 
       <placeholder/> 
      </child> 
      </object> 
      <packing> 
      <property name="expand">False</property> 
      <property name="fill">True</property> 
      <property name="position">2</property> 
      </packing> 
     </child> 
     <child> 
      <object class="GtkBox" id="box2"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="spacing">7</property> 
      <property name="homogeneous">True</property> 
      <child> 
       <object class="GtkProgressBar" id="progress"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       </object> 
       <packing> 
       <property name="expand">False</property> 
       <property name="fill">True</property> 
       <property name="position">0</property> 
       </packing> 
      </child> 
      <child> 
       <object class="GtkButton" id="annulla"> 
       <property name="label" translatable="yes">annulla</property> 
       <property name="visible">True</property> 
       <property name="can_focus">True</property> 
       <property name="receives_default">True</property> 
       </object> 
       <packing> 
       <property name="expand">False</property> 
       <property name="fill">True</property> 
       <property name="position">1</property> 
       </packing> 
      </child> 
      </object> 
      <packing> 
      <property name="expand">False</property> 
      <property name="fill">True</property> 
      <property name="position">3</property> 
      </packing> 
     </child> 
     </object> 
    </child> 
    </object> 
    <object class="GtkBox" id="schermata1"> 
    <property name="visible">True</property> 
    <property name="can_focus">False</property> 
    <property name="orientation">vertical</property> 
    <child> 
     <object class="GtkLabel" id="label1"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <property name="label" translatable="yes">schermata1</property> 
     <property name="ellipsize">start</property> 
     </object> 
     <packing> 
     <property name="expand">False</property> 
     <property name="fill">True</property> 
     <property name="position">0</property> 
     </packing> 
    </child> 
    <child> 
     <placeholder/> 
    </child> 
    </object> 
    <object class="GtkBox" id="schermata2"> 
    <property name="visible">True</property> 
    <property name="can_focus">False</property> 
    <property name="orientation">vertical</property> 
    <child> 
     <object class="GtkLabel" id="label2"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <property name="label" translatable="yes">schermata2</property> 
     </object> 
     <packing> 
     <property name="expand">False</property> 
     <property name="fill">True</property> 
     <property name="position">0</property> 
     </packing> 
    </child> 
    <child> 
     <object class="GtkScrolledWindow" id="scrolled"> 
     <property name="visible">True</property> 
     <property name="can_focus">True</property> 
     <property name="shadow_type">in</property> 
     <property name="min_content_width">400</property> 
     <property name="min_content_height">400</property> 
     <child> 
      <placeholder/> 
     </child> 
     </object> 
     <packing> 
     <property name="expand">True</property> 
     <property name="fill">True</property> 
     <property name="position">1</property> 
     </packing> 
    </child> 
    </object> 
</interface> 

對不起我的英語不好。謝謝:)

+0

我看着你的代碼,但我不能完全確定你想做什麼。代碼和格萊德文件似乎還沒有連接。 – tobias47n9e

回答

0

也許這工作示例將足以讓你開始:

#!/usr/bin/python3 

from gi.repository import Gtk 

from matplotlib.figure import Figure 
from numpy import arange, pi, random, linspace 
import matplotlib.cm as cm 
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas 

class Signals: 
    def on_applicationwindow1_destroy(self, widget): 
     Gtk.main_quit() 

    def nuovoevent(self, toolbutton): 
     pass 

    def okevent(self, toolbutton): 
     pass 

builder = Gtk.Builder() 
builder.add_objects_from_file('test3.glade', ('applicationwindow1', 'schermata1', 'schermata2')) 
builder.connect_signals(Signals()) 

myfirstwindow = builder.get_object('applicationwindow1') 
sw = builder.get_object('scrolledwindow1') 

fig = Figure(figsize=(5,5), dpi=100) 
ax = fig.add_subplot(111, projection='polar') 

N = 20 
theta = linspace(0.0, 2 * pi, N, endpoint=False) 
radii = 10 * random.rand(N) 
width = pi/4 * random.rand(N) 

bars = ax.bar(theta, radii, width=width, bottom=0.0) 

for r, bar in zip(radii, bars): 
    bar.set_facecolor(cm.jet(r/10.)) 
    bar.set_alpha(0.5) 

ax.plot() 

canvas = FigureCanvas(fig) 
sw.add_with_viewport(canvas) 

myfirstwindow.show_all() 
Gtk.main() 

而格萊德文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Generated with glade 3.18.3 --> 
<interface> 
    <requires lib="gtk+" version="3.10"/> 
    <object class="GtkApplicationWindow" id="applicationwindow1"> 
    <property name="can_focus">False</property> 
    <signal name="destroy" handler="on_applicationwindow1_destroy" swapped="no"/> 
    <child> 
     <object class="GtkBox" id="box1"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <property name="orientation">vertical</property> 
     <child> 
      <object class="GtkMenuBar" id="menubar1"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <child> 
       <object class="GtkMenuItem" id="menuitem1"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="label" translatable="yes">_File</property> 
       <property name="use_underline">True</property> 
       <child type="submenu"> 
        <object class="GtkMenu" id="menu1"> 
        <property name="visible">True</property> 
        <property name="can_focus">False</property> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem1"> 
         <property name="label">gtk-new</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem2"> 
         <property name="label">gtk-open</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem3"> 
         <property name="label">gtk-save</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem4"> 
         <property name="label">gtk-save-as</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkSeparatorMenuItem" id="separatormenuitem1"> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem5"> 
         <property name="label">gtk-quit</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        </object> 
       </child> 
       </object> 
      </child> 
      <child> 
       <object class="GtkMenuItem" id="menuitem2"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="label" translatable="yes">_Modifica</property> 
       <property name="use_underline">True</property> 
       <child type="submenu"> 
        <object class="GtkMenu" id="menu2"> 
        <property name="visible">True</property> 
        <property name="can_focus">False</property> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem6"> 
         <property name="label">gtk-cut</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem7"> 
         <property name="label">gtk-copy</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem8"> 
         <property name="label">gtk-paste</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem9"> 
         <property name="label">gtk-delete</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        </object> 
       </child> 
       </object> 
      </child> 
      <child> 
       <object class="GtkMenuItem" id="menuitem3"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="label" translatable="yes">_Visualizza</property> 
       <property name="use_underline">True</property> 
       </object> 
      </child> 
      <child> 
       <object class="GtkMenuItem" id="menuitem4"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       <property name="label" translatable="yes">A_iuto</property> 
       <property name="use_underline">True</property> 
       <child type="submenu"> 
        <object class="GtkMenu" id="menu3"> 
        <property name="visible">True</property> 
        <property name="can_focus">False</property> 
        <child> 
         <object class="GtkImageMenuItem" id="imagemenuitem10"> 
         <property name="label">gtk-about</property> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <property name="use_underline">True</property> 
         <property name="use_stock">True</property> 
         </object> 
        </child> 
        </object> 
       </child> 
       </object> 
      </child> 
      </object> 
      <packing> 
      <property name="expand">False</property> 
      <property name="fill">True</property> 
      <property name="position">0</property> 
      </packing> 
     </child> 
     <child> 
      <object class="GtkButtonBox" id="buttonbox1"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="homogeneous">True</property> 
      <property name="layout_style">spread</property> 
      <child> 
       <object class="GtkButton" id="Nuovo"> 
       <property name="label" translatable="yes">Nuovo</property> 
       <property name="visible">True</property> 
       <property name="can_focus">True</property> 
       <property name="receives_default">True</property> 
       <signal name="clicked" handler="nuovoevent" swapped="no"/> 
       </object> 
       <packing> 
       <property name="expand">False</property> 
       <property name="fill">True</property> 
       <property name="position">0</property> 
       </packing> 
      </child> 
      <child> 
       <object class="GtkButton" id="ok"> 
       <property name="label" translatable="yes">Ok</property> 
       <property name="visible">True</property> 
       <property name="can_focus">True</property> 
       <property name="receives_default">True</property> 
       <signal name="clicked" handler="okevent" swapped="no"/> 
       </object> 
       <packing> 
       <property name="expand">False</property> 
       <property name="fill">True</property> 
       <property name="position">1</property> 
       </packing> 
      </child> 
      </object> 
      <packing> 
      <property name="expand">False</property> 
      <property name="fill">True</property> 
      <property name="position">1</property> 
      </packing> 
     </child> 
     <child> 
      <object class="GtkBox" id="containerbox"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="orientation">vertical</property> 
      <child> 
       <object class="GtkScrolledWindow" id="scrolledwindow1"> 
       <property name="visible">True</property> 
       <property name="can_focus">True</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">0</property> 
       </packing> 
      </child> 
      </object> 
      <packing> 
      <property name="expand">True</property> 
      <property name="fill">True</property> 
      <property name="position">2</property> 
      </packing> 
     </child> 
     <child> 
      <object class="GtkBox" id="box2"> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="spacing">7</property> 
      <property name="homogeneous">True</property> 
      <child> 
       <object class="GtkProgressBar" id="progress"> 
       <property name="visible">True</property> 
       <property name="can_focus">False</property> 
       </object> 
       <packing> 
       <property name="expand">False</property> 
       <property name="fill">True</property> 
       <property name="position">0</property> 
       </packing> 
      </child> 
      <child> 
       <object class="GtkButton" id="annulla"> 
       <property name="label" translatable="yes">annulla</property> 
       <property name="visible">True</property> 
       <property name="can_focus">True</property> 
       <property name="receives_default">True</property> 
       </object> 
       <packing> 
       <property name="expand">False</property> 
       <property name="fill">True</property> 
       <property name="position">1</property> 
       </packing> 
      </child> 
      </object> 
      <packing> 
      <property name="expand">False</property> 
      <property name="fill">True</property> 
      <property name="position">3</property> 
      </packing> 
     </child> 
     </object> 
    </child> 
    </object> 
    <object class="GtkBox" id="schermata1"> 
    <property name="visible">True</property> 
    <property name="can_focus">False</property> 
    <property name="orientation">vertical</property> 
    <child> 
     <object class="GtkLabel" id="label1"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <property name="label" translatable="yes">schermata1</property> 
     <property name="ellipsize">start</property> 
     </object> 
     <packing> 
     <property name="expand">False</property> 
     <property name="fill">True</property> 
     <property name="position">0</property> 
     </packing> 
    </child> 
    <child> 
     <placeholder/> 
    </child> 
    </object> 
    <object class="GtkBox" id="schermata2"> 
    <property name="visible">True</property> 
    <property name="can_focus">False</property> 
    <property name="orientation">vertical</property> 
    <child> 
     <object class="GtkLabel" id="label2"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <property name="label" translatable="yes">schermata2</property> 
     </object> 
     <packing> 
     <property name="expand">False</property> 
     <property name="fill">True</property> 
     <property name="position">0</property> 
     </packing> 
    </child> 
    <child> 
     <object class="GtkScrolledWindow" id="scrolled"> 
     <property name="visible">True</property> 
     <property name="can_focus">True</property> 
     <property name="shadow_type">in</property> 
     <property name="min_content_width">400</property> 
     <property name="min_content_height">400</property> 
     <child> 
      <placeholder/> 
     </child> 
     </object> 
     <packing> 
     <property name="expand">True</property> 
     <property name="fill">True</property> 
     <property name="position">1</property> 
     </packing> 
    </child> 
    </object> 
</interface>