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

发布于 2024-12-15 17:02:20 字数 6344 浏览 0 评论 0原文

我整个早上都在尝试调试这个问题,但似乎找不到解决方案。我有一个简单的 Glade/PyGTK 脚本,其中包含三个复选框和一个提交按钮。我一直在尝试确保我使用 Glade GTKObject 的销毁信号为 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 文件 (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>

I've been attempting to debug this all morning but can't seem to find the solution. I have a simple Glade/PyGTK script with three check boxes and a submit button. I've been trying to make sure I have a proper handler setup for gtk.main_quit with Glade GTKObject's Destroy Signal, but even after setting up correctly in Glade, my Python script doesn't detect the handler, hangs the application, and returns this error.

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

I've tried changing the the handlers name, and even completely restarting the script from scratch to see where I went wrong. I can't seem to find it. Any help is appreciated! Thanks in advance.

Python Script:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萧瑟寒风 2024-12-22 17:02:20

我真的不明白你在用信号字典做什么。您说#创建我们的字典并连接它,但您似乎从未“连接”它。

如果你只是将它们的名称 self.Destroy 更改为 self.on_MainWindow_destroy 它对我来说效果很好。

builder.connect_signals 方法在空地文件中查找信号,并将它们与脚本中同名的函数进行匹配。我不知道有什么方法可以用字典来做到这一点,但如果有的话,你似乎还没有实现它。

I don't really understand what you're doing with your signal dictionary. You say #Create our dictionary and connect it, but you never seem to 'connect' it.

If you just change them name self.Destroy to self.on_MainWindow_destroy it works fine for me.

The builder.connect_signals method looks for signals in the glade file and matches them functions of the same name in your script. I don't know of any way to do this with a dictionary, but if there is, you don't seem to have implemented it.

乱了心跳 2024-12-22 17:02:20

connect_signals() 调用之前创建字典 dic,然后说

self.builder.connect_signals(dic)

connect_signals() 的第一个参数必须是字典或处理程序映射处理程序的名称。 PyGTK 中的一个常见习惯用法是将 self 作为此字典传递,因为类自动也​​是映射到函数的函数名称的字典。然而,要使其发挥作用,类中的函数必须与 Glade 文件中的处理程序具有相同的名称,正如 root45 指出的那样。如果您不想或不能给它们提供相同的名称,请使用字典。

Create your dictionary dic before the connect_signals() call and then say

self.builder.connect_signals(dic)

The first argument to connect_signals() has to be a dictionary or mapping of handler names to handlers. A common idiom in PyGTK is to pass self as this dictionary, since a class is automatically also a dictionary of function names mapped to functions. However, for this to work, the functions in your class have to have the same names as the handlers in the Glade file, as root45 points out. If you don't want to or can't give them the same names, then use a dictionary.

待天淡蓝洁白时 2024-12-22 17:02:20

我也遇到同样的情况。当我将 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()

The same happened to me. The problem disapear when i separated the init() part from the show() part. Maybe gtk / python need the initialisation to be done to fully detect the class functions.

   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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文