Python / Glade / PyGTK - 没有正确检测处理程序

时间:2011-11-15 18:31:28

标签: python gtk pygtk glade

我整个上午都试图调试这个,但似乎无法找到解决方案。我有一个简单的Glade / PyGTK脚本,带有三个复选框和一个提交按钮。我一直在努力确保使用Glade GTKObject的Destroy Signal对gtk.main_quit进行适当的处​​理程序设置,但即使在Glade中正确设置后,我的Python脚本也没有检测到处理程序,挂起应用程序,并返回此错误。

E:\Projects\DED\test.py:34: RuntimeWarning: missing handler 'on_MainWindow_destroy'
  self.builder.connect_signals(self)

我已经尝试更改处理程序名称,甚至从头开始完全重新启动脚本以查看我出错的地方。我似乎找不到它。任何帮助表示赞赏!提前谢谢。

Python脚本:

import sys
try:
    import pygtk
    pygtk.require("2.0")
except:
    pass
try:
    import gtk
    import gtk.glade
except:
    sys.exit(1)


class GladeTest:

    def __init__(self):

        #Set the Glade file
        filename = "gui.glade"
        self.builder = gtk.Builder()
        self.builder.add_from_file(filename)
        self.builder.connect_signals(self)

        #Create our dictionay and connect it
        dic = { "btnSubmit_clicked" : self.btnSubmit_clicked,
        "on_MainWindow_destroy" : self.Destroy }

    def btnSubmit_clicked(self, widget):
        chkbt_chrome = self.builder.get_object("chkboxDropbox")
        print "ACTIVE--",chkbt_chrome.get_active()


    def Destroy(self, obj):
        gtk.main_quit() #make the program quit



if __name__ == "__main__":
    GladeTest()
    gtk.main()

print "All Done"

Glade File(gui.glade):

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="2.24"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="MainWindow">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">MainWindow</property>
    <property name="resizable">False</property>
    <property name="window_position">center</property>
    <signal name="destroy" handler="on_MainWindow_destroy" swapped="no"/>
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Downloader</property>
            <attributes>
              <attribute name="style" value="normal"/>
              <attribute name="size" value="300"/>
            </attributes>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="chkboxDropbox">
            <property name="label" translatable="yes">Dropbox</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="use_action_appearance">False</property>
            <property name="draw_indicator">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="chkboxPython">
            <property name="label" translatable="yes">Python</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="use_action_appearance">False</property>
            <property name="draw_indicator">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="chkboxChrome">
            <property name="label" translatable="yes">Google Chrome</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="use_action_appearance">False</property>
            <property name="draw_indicator">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">3</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="btnSubmit">
            <property name="label" translatable="yes">Download/Run</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="clicked" handler="btnSubmit_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">4</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

3 个答案:

答案 0 :(得分:2)

我真的不明白你在使用信号词典做什么。你说#Create our dictionary and connect it,但你似乎永远不会“连接”它。

如果您只是将self.Destroy更改为self.on_MainWindow_destroy,则对我来说效果很好。

builder.connect_signals方法在glade文件中查找信号,并在脚本中匹配它们的同名函数。我不知道用字典做任何方法,但如果有,你似乎没有实现它。

答案 1 :(得分:0)

dic电话会议之前创建您的字典connect_signals(),然后说

self.builder.connect_signals(dic)

connect_signals()的第一个参数必须是处理程序名称与处理程序的字典或映射。 PyGTK中的一个常见习惯是将self作为此字典传递,因为类自动也​​是映射到函数的函数名字典。但是,为了实现这一点,类中的函数必须与Glade文件中的处理程序具有相同的名称,如root45所指出的那样。如果您不想或不能给它们相同的名称,那么请使用字典。

答案 2 :(得分:0)

同样的事发生在我身上。当我将init()部分与show()部分分开时,问题消失了。也许gtk / python需要进行初始化才能完全检测类函数。

   class MyApp():
     ...
     def __init__(self):
        try:
            builder = gtk.Builder()
            builder.add_from_file("carte.glade")
        except:
            self.error_message("Failed to load UI XML file: carte.glade")
            sys.exit(1)

       # connect signals
        builder.connect_signals(self)        

     def main(self):
        self.window.show()
        gtk.main()