简体   繁体   English

把glade接口放在python中

[英]putting glade interface in python

I've made a gui in glade that I want to put in a python program. 我已经在林间空间做了一个gui,我想把它放在一个python程序中。 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). 我正在调整我在网上找到的教程中的说明,以加载我的glade文件(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. 我还看了一下(http://www.linuxjournal.com/article/6586?page=0,2),它的功能稍有不同(“self.wTree = gtk.glade.XML(gladefile, windowname)“而不是没有windowname”,并实现了与我的等价物并没有解决它。 I definitely have pygtk working, I made something without using glade before and it worked fine. 我肯定有pygtk工作,我之前没有使用glade制作的东西,它工作得很好。 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. 我正在运行xubuntu 11.04。 The Murrine configuration thing comes up when any gtk application opens, but I included it in case it is relevant. 当任何gtk应用程序打开时,Murrine配置会出现,但是我将它包括在内以防它相关。 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()

Try with this code: 试试这段代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade

class HellowWorldGTK:

    def __init__(self):
        self.gladefile = "helloworld.glade" 
        self.glade = gtk.Builder()
        self.glade.add_from_file(self.gladefile)
        self.glade.connect_signals(self)
        self.glade.get_object("MainWindow").show_all()

    def on_MainWindow_delete_event(self, widget, event):
        gtk.main_quit()

if __name__ == "__main__":
    try:
        a = HellowWorldGTK()
        gtk.main()
    except KeyboardInterrupt:
        pass

Remember: In Glade, Edit the "Preferences" of the file to " GTKBuilder " (not "libglade") 记住:在Glade中,将文件的“首选项”编辑为“ GTKBuilder ”(不是“libglade”)

Your PyHelloWorld.glade is incorrect. 你的PyHelloWorld.glade不正确。 Make sure you created it with the correct Glade application, there are Glade2 and Glade3 applications that can be installed and used. 确保使用正确的Glade应用程序创建它,可以安装和使用Glade2和Glade3应用程序。 If you downloaded the file make sure it is correct. 如果您下载了文件,请确保它是正确的。 The error message says it all: 错误消息说明了一切:

Expected <glade-interface>.  Got  <interface>

So the XML file has the interface tag, but PyGTK library expects glade-interface tag. 因此XML文件具有接口标记,但PyGTK库需要glade-interface标记。

Since I always end up having problems with this, here is a Python 2.7 code that I use for one or the other: 由于我总是遇到这个问题,这里有一个Python 2.7代码,我用于其中一个:

for Libglade : 对于Libglade

# needs libglade (not for gtk-builder)
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade

gladefile = "test-libglade.glade"
wTree = gtk.glade.XML(gladefile)
window = wTree.get_widget("MainWindow")
if (window):
  window.connect("destroy", gtk.main_quit)

window.show_all() # must have!
gtk.main()

For GtkBuilder : 对于GtkBuilder

# needs gtk-builder (not for libglade)
import pygtk
pygtk.require("2.0")
import gtk

gladefile = "test-gtkbuilder.glade"
wTree = gtk.Builder()
wTree.add_from_file(gladefile)
window = wTree.get_object("MainWindow")
if (window):
  window.connect("destroy", gtk.main_quit)

window.show_all() # must have!
gtk.main()

In Glade, you can just add a Window, call it MainWindow , and save two versions with the respective filenames as above for each format; 在Glade中,您只需添加一个Window,将其命名为MainWindow ,并为每种格式保存两个版本,其中包含上述相应的文件名; and these snippets should work with them respeactively. 这些片段应该与他们一起工作。

Hope this helps someone, 希望这有助于某人,
Cheers! 干杯!

This works perfectly. 这非常有效。

#!/usr/bin/python
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
class SubinsWindow:
 def __init__(self):
  self.gladefile = "game.glade"
  self.glade = gtk.Builder()
  self.glade.add_from_file(self.gladefile)
  self.glade.connect_signals(self)
  self.win=self.glade.get_object("window1") # Window Name in GLADE
  self.win.show_all()

if __name__ == "__main__":
 a = SubinsWindow()
 gtk.main()

If you are using GTK+3 in python, see builder . 如果您在python中使用GTK + 3,请参阅builder

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

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

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

builder = Gtk.Builder()
builder.add_from_file("builder_example.glade")
builder.connect_signals(Handler())

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

Gtk.main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM