在glade和python中为GtkFileChooser Button实现get_filename()方法时遇到问题?

发布于 2024-10-18 10:11:41 字数 5172 浏览 2 评论 0原文

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

<?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()

正确的做法是什么???

As a newbee I am trying to create a basic theme installer using Glade and python. Everything is working fine but i am not able to find anywhere how to use get_filename() method for gtkFileChooserButton.
My Glade File Looks Like this.

<?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>

`

And Python Code is:

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()

what is the right way of doing it???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山川志 2024-10-25 10:11:41

我相信,如果您从构建器获取 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)

希望这有帮助,问候

I believe if you would get your GtkFileChooserButton object from the builder and save a reference to it as a member of workplz class you could query it in the add signal handler. See if script below would work for you:

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)

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文