Python/Glade/PyGTK - 未正确检测处理程序
我整个早上都在尝试调试这个问题,但似乎找不到解决方案。我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我真的不明白你在用信号字典做什么。您说
#创建我们的字典并连接它
,但您似乎从未“连接”它。如果你只是将它们的名称
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
toself.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.在
connect_signals()
调用之前创建字典dic
,然后说connect_signals()
的第一个参数必须是字典或处理程序映射处理程序的名称。 PyGTK 中的一个常见习惯用法是将self
作为此字典传递,因为类自动也是映射到函数的函数名称的字典。然而,要使其发挥作用,类中的函数必须与 Glade 文件中的处理程序具有相同的名称,正如 root45 指出的那样。如果您不想或不能给它们提供相同的名称,请使用字典。Create your dictionary
dic
before theconnect_signals()
call and then sayThe first argument to
connect_signals()
has to be a dictionary or mapping of handler names to handlers. A common idiom in PyGTK is to passself
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.我也遇到同样的情况。当我将 init() 部分与 show() 部分分开时,问题就消失了。也许 gtk / python 需要进行初始化才能完全检测类函数。
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.