0

作为一个新手,我正在尝试使用 Glade 和 python 创建一个基本的主题安装程序。一切正常,但我无法在任何地方找到如何为 gtkFileChooserButton 使用 get_filename() 方法。我的 Glade 文件看起来像这样。

<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="main">
<property name="title" translatable="yes">Maininin</property>
<property name="window_position">center</property>
<signal name="destroy" handler="on_main_destroy"/>
<child>
  <object class="GtkVBox" id="vbox1">
    <property name="visible">True</property>
    <child>
      <object class="GtkLabel" id="INFO">
        <property name="visible">True</property>
        <property name="label" translatable="yes">Theme</property>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="padding">28</property>
        <property name="position">0</property>
      </packing>
    </child>
    <child>
      <object class="GtkHButtonBox" id="hbuttonbox1">
        <property name="visible">True</property>
        <property name="layout_style">spread</property>
        <child>
          <object class="GtkFileChooserButton" id="DD">
            <property name="visible">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="pack_type">end</property>
            <property name="position">0</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="position">1</property>
      </packing>
    </child>
    <child>
      <object class="GtkHButtonBox" id="hbuttonbox2">
        <property name="visible">True</property>
        <property name="layout_style">spread</property>
        <child>
          <object class="GtkButton" id="Add">
            <property name="label">gtk-add</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_stock">True</property>
            <signal name="clicked" handler="on_Add_clicked"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="Quit">
            <property name="label">gtk-quit</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_stock">True</property>
            <signal name="clicked" handler="on_Quit_clicked"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="pack_type">end</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="position">2</property>
      </packing>
    </child>
    <child>
      <object class="GtkLabel" id="WARN">
        <property name="visible">True</property>
        <property name="label" translatable="yes">Warning</property>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="padding">37</property>
        <property name="position">3</property>
      </packing>
    </child>
  </object>
</child>

`

Python代码是:

import sys
try:  
import pygtk  
pygtk.require("2.0")  
except:  
pass  
try:  
import gtk   
except:  
print("GTK Not Availible")
sys.exit(1)


class workplz(object):


def __init__( self ):
    builder = gtk.Builder()
    builder.add_from_file("heme.glade")
    builder.connect_signals({"on_main_destroy" : self.quit, "on_Add_clicked" : self.add, "on_Quit_clicked" : self.quit})
    self._window = builder.get_object("main")
    self._window.show()     

def add(self, widget):
    target=DD.get_filename()
    print target


def quit(self, widget):
    sys.exit(0)


if __name__ == "__main__":
    wine = workplz()
    gtk.main()

什么是正确的做法???

4

1 回答 1

0

我相信如果您从构建器获取 GtkFileChooserButton 对象并将对它的引用保存为类成员,您可以在信号处理程序workplz中查询它。add看看下面的脚本是否适合你:

class workplz(object):
    def __init__( self ):
        builder = gtk.Builder()
        builder.add_from_file("heme.glade")
        builder.connect_signals({"on_main_destroy" : self.quit, "on_Add_clicked" : self.add, "on_Quit_clicked" : self.quit})

        # get GtkFileChooserButton object 
        self.DD = builder.get_object('DD')

        self._window = builder.get_object("main")
        self._window.show()     

    def add(self, widget):
        target = self.DD.get_filename()
        print target

    def quit(self, widget):
        sys.exit(0)

希望这会有所帮助,问候

于 2011-02-21T02:41:23.680 回答