将glade接口放入python中
我在 Glade 中制作了一个 gui,我想将其放入 python 程序中。我正在调整我在网上找到的教程中的说明,以将其加载到我的林间空地文件中(http://www.pygtk.org/articles/pygtk-glade-gui/Creating_a_GUI_using_PyGTK_and_Glade.htm)。当我遇到问题时,我尝试了一些基本的(一键),将其称为与该教程中相同的东西,并复制粘贴他们的代码,但它仍然不起作用。我还看了一下(http://www.linuxjournal.com/article/6586?page=0,2),它的函数调用方式略有不同(“self.wTree=gtk.glade.XML(gladefile, windowname)" 而不是没有 windowname),并用我的实现了等效的方法,但这并没有修复它。我肯定有 pygtk 工作,我以前没有使用空地做了一些东西,它工作得很好。我收到的错误是:
/usr/share/themes/NOX/gtk-2.0/gtkrc:233: Murrine configuration option "gradients"
is no longer supported and will be ignored.
(helloWorld.py:9804): libglade-WARNING **: Expected <glade-interface>. Got
<interface>.
(helloWorld.py:9804): libglade-WARNING **: did not finish in PARSER_FINISH state
Traceback (most recent call last):
File "helloWorld.py", line 31, in <module>
hwg = HellowWorldGTK()
File "helloWorld.py", line 22, in __init__
self.wTree = gtk.glade.XML(self.gladefile)
RuntimeError: could not create GladeXML object
我正在运行 xubuntu 11.04。当任何 gtk 应用程序打开时,Murrine 配置就会出现,但我将其包含在内,以防它相关。这是我从教程中获取的代码(但不起作用)
#!/usr/bin/env python
import sys
try:
import pygtk
pygtk.require("2.0")
except:
pass
try:
import gtk
import gtk.glade
except:
sys.exit(1)
class HellowWorldGTK:
"""This is an Hello World GTK application"""
def __init__(self):
#Set the Glade file
self.gladefile = "PyHelloWorld.glade"
self.wTree = gtk.glade.XML(self.gladefile)
#Get the Main Window, and connect the "destroy" event
self.window = self.wTree.get_widget("MainWindow")
self.window.show()
if (self.window):
self.window.connect("destroy", gtk.main_quit)
if __name__ == "__main__":
hwg = HellowWorldGTK()
gtk.main()
I've made a gui in glade that I want to put in a python program. I was adapting the instructions from a tutorial I found online to load in my glade file (http://www.pygtk.org/articles/pygtk-glade-gui/Creating_a_GUI_using_PyGTK_and_Glade.htm). When I had problems I tried something basic (one button) calling it the same thing as in that tutorial, and copy pasting their code, and it still didn't work. I also took a look at (http://www.linuxjournal.com/article/6586?page=0,2), which has a function being called slightly differently ("self.wTree=gtk.glade.XML (gladefile,windowname)" instead of without windowname), and implemented an equivalent with mine and that didn't fix it. I definitely have pygtk working, I made something without using glade before and it worked fine. The error I'm getting is:
/usr/share/themes/NOX/gtk-2.0/gtkrc:233: Murrine configuration option "gradients"
is no longer supported and will be ignored.
(helloWorld.py:9804): libglade-WARNING **: Expected <glade-interface>. Got
<interface>.
(helloWorld.py:9804): libglade-WARNING **: did not finish in PARSER_FINISH state
Traceback (most recent call last):
File "helloWorld.py", line 31, in <module>
hwg = HellowWorldGTK()
File "helloWorld.py", line 22, in __init__
self.wTree = gtk.glade.XML(self.gladefile)
RuntimeError: could not create GladeXML object
I'm running xubuntu 11.04. The Murrine configuration thing comes up when any gtk application opens, but I included it in case it is relevant. Here's the code I took from the tutorial (but isn't working)
#!/usr/bin/env python
import sys
try:
import pygtk
pygtk.require("2.0")
except:
pass
try:
import gtk
import gtk.glade
except:
sys.exit(1)
class HellowWorldGTK:
"""This is an Hello World GTK application"""
def __init__(self):
#Set the Glade file
self.gladefile = "PyHelloWorld.glade"
self.wTree = gtk.glade.XML(self.gladefile)
#Get the Main Window, and connect the "destroy" event
self.window = self.wTree.get_widget("MainWindow")
self.window.show()
if (self.window):
self.window.connect("destroy", gtk.main_quit)
if __name__ == "__main__":
hwg = HellowWorldGTK()
gtk.main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试使用以下代码:
记住:
在 Glade 中,将文件的“首选项”编辑为“GTKBuilder”(而不是“libglade”)
Try with this code:
Remember:
In Glade, Edit the "Preferences" of the file to "GTKBuilder" (not "libglade")
您的 PyHelloWorld.glade 不正确。确保您使用正确的 Glade 应用程序创建它,有可以安装和使用的 Glade2 和 Glade3 应用程序。如果您下载了该文件,请确保它是正确的。错误消息说明了一切:
因此 XML 文件具有接口标记,但 PyGTK 库需要glade-interface 标记。
Your PyHelloWorld.glade is incorrect. Make sure you created it with the correct Glade application, there are Glade2 and Glade3 applications that can be installed and used. If you downloaded the file make sure it is correct. The error message says it all:
So the XML file has the interface tag, but PyGTK library expects glade-interface tag.
由于我总是遇到这方面的问题,这里是我用于其中之一的 Python 2.7 代码:
对于
Libglade
:对于
GtkBuilder
:在 Glade 中,您可以只需添加一个窗口,将其命名为
MainWindow
,并为每种格式保存两个版本,并使用上述各自的文件名;这些片段应该分别与它们一起工作。希望这对某人有帮助,
干杯!
Since I always end up having problems with this, here is a Python 2.7 code that I use for one or the other:
for
Libglade
:For
GtkBuilder
:In Glade, you can just add a Window, call it
MainWindow
, and save two versions with the respective filenames as above for each format; and these snippets should work with them respeactively.Hope this helps someone,
Cheers!
如果您在 python 中使用 GTK+3,请参阅 builder。
If you are using GTK+3 in python, see builder.
这非常有效。
This works perfectly.