2014-02-11 66 views
0

我想用Python,GTK + 3和格萊德寫一個GUI。在教程之後,我製作了一些代碼。問題似乎是connect_signals()不起作用,所以點擊按鈕不起作用。Python,GTK + 3和格萊德 - connect_signals()不工作

蟒蛇:

from gi.repository import Gtk 

class Handler: 
    def onDeleteWindow(self, *args): 
     Gtk.main_quit(*args) 

    def onButtonPressed(self, button): 
     print("Hello World!") 

builder = Gtk.Builder() 
builder.add_from_file("gui.glade") 

builder.connect_signals(Handler()) 

window = builder.get_object("window1") 
window.show_all() 

Gtk.main() 

空地:

<?xml version="1.0" encoding="UTF-8"?> 
<interface> 
    <!-- interface-requires gtk+ 3.0 --> 
    <object class="GtkWindow" id="window1"> 
    <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> 
     <property name="spacing">4</property> 
     <child> 
       <object class="GtkButton" id="button1"> 
       <property name="label" translatable="yes">Install</property> 
       <property name="use_action_appearance">False</property> 
       <property name="visible">True</property> 
       <property name="can_focus">True</property> 
       <property name="receives_default">True</property> 
       <property name="use_action_appearance">False</property> 
       </object> 
     </child> 
     </object> 
    </child> 
    </object> 
</interface> 

回答

3

您已在gui.glade文件錯過了信號名稱: 看到兩個標籤叫下面的信號。

參考:http://python-gtk-3-tutorial.readthedocs.org/en/latest/builder.html

<?xml version="1.0" encoding="UTF-8"?> 
<interface> 
    <!-- interface-requires gtk+ 3.0 --> 
    <object class="GtkWindow" id="window1"> 
    <property name="can_focus">False</property> 
    <signal name="delete-event" handler="onDeleteWindow" swapped="no"/> 
    <child> 
     <object class="GtkBox" id="box1"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <property name="orientation">vertical</property> 
     <property name="spacing">4</property> 
     <child> 
       <object class="GtkButton" id="button1"> 
       <property name="label" translatable="yes">Install</property> 
       <property name="use_action_appearance">False</property> 
       <property name="visible">True</property> 
       <property name="can_focus">True</property> 
       <property name="receives_default">True</property> 
       <property name="use_action_appearance">False</property> 
       <signal name="pressed" handler="onButtonPressed" swapped="no"/> 
       </object> 
     </child> 
     </object> 
    </child> 
    </object> 
</interface> 
+0

HUP,僅此而已。非常感謝! – Turbinenreiter