0

gui.glade:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="window">
    <property name="can_focus">False</property>
    <signal name="destroy" handler="on_window_destroy" swapped="no"/>
    <child>
      <object class="GtkLabel" id="label">
        <property name="width_request">250</property>
        <property name="height_request">100</property>
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">Hello, World!</property>
      </object>
    </child>
  </object>
</interface>

gui.py:

#!/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 GTKWindow:
    def __init__(self):
        self.wTree = gtk.glade.XML("gui.glade")
        self.window = self.wTree.get_widget("window")

        if (self.window):
            self.window.connect("on_window_destroy", gtk.main_quit)

        gtk.main()

if __name__ == "__main__":
    window = GTKWindow()

终端错误:

(gui.py:4504): libglade-WARNING **: Expected <glade-interface>.  Got <interface>.

(gui.py:4504): libglade-WARNING **: did not finish in PARSER_FINISH state
Traceback (most recent call last):
  File "gui.py", line 26, in <module>
    window = GTKWindow()
  File "gui.py", line 17, in __init__
    self.wTree = gtk.glade.XML("gui.glade")
RuntimeError: could not create GladeXML object

我确保将 glade 文件保存在 Libglade 下,但是我无法让项目运行(请参阅上面终端中的错误)。我已经尝试使用 gtk.Builder 并且即使我将 glade 文件保存在 GtkBuilder 文件下也没有任何反应。有任何想法吗?

4

1 回答 1

0

我对您的代码进行了一些更改。我希望对你有所帮助。

桂格莱德

 <?xml version="1.0" encoding="UTF-8"?> <glade-interface>   <!--
 interface-requires gtk+ 2.24 -->   <!-- interface-naming-policy
 project-wide -->   <widget class="GtkWindow" id="window">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
     <signal name="destroy" handler="on_window_destroy" after="yes" swapped="no"/>
     <child>
       <widget class="GtkLabel" id="label1">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="label" translatable="yes">Hello, World!</property>
       </widget>
     </child>   </widget> </glade-interface>

gui.py

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

class GTKWindow():
    def __init__(self):
        self.wTree = gtk.glade.XML("gui.glade")        
        dict = {
          "on_window1_destroy": self.destroy,                   
          }   
        self.wTree.signal_autoconnect(dict)
    def destroy(self,*args):
        gtk.main_quit()

#    def    
if __name__ == "__main__":
    window = GTKWindow()
gtk.main()
于 2013-04-15T14:40:24.167 回答