简体   繁体   English

Glade 3中的自定义python小部件

[英]Custom python widgets in glade 3

I would like to subclass an existing Gtk widget and get it to work with Glade3. 我想继承一个现有的Gtk小部件并使其与Glade3一起使用。 The code is all python, and I'm using PyGObject. 代码全部是python,我正在使用PyGObject。

What I want to do is extend Gtk.TextView and be able to add it to my glade UI as if it were a native widget. 我想要做的是扩展Gtk.TextView并能够将其添加到我的空地用户界面,就好像它是本机小部件一样。

I read that it was possible with Glade 2, but I'm not sure how to do this with Glade 3, and I can't seem to find any examples or documentation on this. 我读到Glade 2是有可能的,但是我不确定Glade 3如何做到这一点,而且我似乎找不到任何示例或文档。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Finally found out how to do it. 终于找到了怎么做。 First, you have to declare that your glade file depends upon a catalog . 首先,您必须声明您的林间空地文件依赖于catalog

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <!-- interface-requires the_catalog_name 1.0 -->
  ...Rest of the glade file...

Next, create a catalog.xml file. 接下来,创建一个catalog.xml文件。 To extend a treeview, it should look something like this: 要扩展树视图,它应该看起来像这样:

<glade-catalog name="the_catalog_name" domain="glade-3" 
               depends="gtk+" version="1.0">
  <glade-widget-classes>
    <glade-widget-class title="My Custom Text View" name="CustomTextView" 
                        generic-name="CustomTextView" parent="GtkTextView"
                        icon-name="widget-gtk-textview"/>
  </glade-widget-classes>
</glade-catalog>

And that's it. 就是这样。 Obviously There's no python code written yet, but that won't prevent Glade from displaying your custom widget. 显然,还没有编写python代码,但这不会阻止Glade显示您的自定义小部件。 It will just treat it as a normal TextView, Label, or whatever else you want to extend. 它将仅将其视为普通的TextView,Label或您要扩展的任何其他内容。

Oh, and I couldn't find a way in Glade itself to create a CustomTextView unfortunately. 哦,很遗憾,我无法在Glade中找到创建CustomTextView的方法。 But what you can do is add a regular TextView in glade, save the glade file and open it in a text editor. 但是您可以做的是在林间空地中添加常规TextView,保存林间空地文件并在文本编辑器中将其打开。 Just look for the treeview you created and change like so: 只需查找您创建的treeview并进行如下更改:

-              <object class="TreeView" id="treeview1">
+              <object class="CustomTreeView" id="treeview1">

Open it back up in glade and it shows up. 在林间打开并将其显示出来。 Then just define a CustomTreeView class in python and extend as needed. 然后,只需在python中定义CustomTreeView类并根据需要扩展即可。

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

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