简体   繁体   English

迁移到pygobject,glade和gtk3时,请使用条目组合框

[英]Use entry combobox when migrating to pygobject, glade and gtk3

I develop in python with glade and pygtk since 3 months, but even before I had time to get used to it, it was already obsolete. 从3个月开始,我就使用Glade和pygtk在python中进行开发,但是即使在我没有时间习惯之前,它已经过时了。

Using Archlinux, my system is constantly up to date, so I am forced to use gtk3 even if I found it a bit lacking of features compared to gtk2. 使用Archlinux,我的系统一直处于最新状态,因此即使我发现它与gtk2相比缺少一些功能,也不得不使用gtk3。

So I decided to switch to pygobject. 所以我决定切换到pygobject。 Unfortunately, the documentation is not complete. 不幸的是,文档不完整。

I successfully upgraded my glade file and my python code to the new system, but one error subsists. 我已成功将我的林间空地文件和python代码升级到新系统,但是仍然存在一个错误。

In one of my programs, I have a combobox with an entry. 在我的一个程序中,我有一个带有条目的组合框。 I use to call the method get_active_text() to get the content of the entry, regardless if it was selected from the combobox or entered by the user. 我曾经调用过get_active_text()方法来获取条目的内容,而不管它是从组合框中选择还是由用户输入。

This method does not exist any more (I suppose, because it gave me an error) so I use this instead : 这个方法已经不存在了(我想是因为它给了我一个错误),所以我改用这个方法:

def get_license(self):
    #return self.combobox_license.get_active_text()
    tree_iter = self.combobox_license.get_active_iter()
    if tree_iter != None:
        model = self.combobox_license.get_model()
        return model[tree_iter][0]
    else:
        entry = self.combobox_license.get_child()
        return entry.get_text()

As you can see the old code is commented. 如您所见,旧代码已注释。

This code works, but I have an odd issue : I can't use the entry ! 这段代码有效,但是我有一个奇怪的问题:我不能使用该条目!

I am able to select the text from the combobox, but the entry is not usable. 我可以从组合框中选择文本,但是该条目不可用。 I can select, but I can't type in it. 我可以选择,但不能输入。

Is this a new behavior I need to activate somewhere ? 这是我需要在某处激活的新行为吗? With the gtk2 version of the program, I don't have any problem. 使用该程序的gtk2版本,我没有任何问题。

Here is the part in my glade file that describes the combobox entry : 这是我的林间空地文件中描述组合框条目的部分:

  <object class="GtkComboBox" id="combobox_license">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="model">liststore_license</property>
    <property name="has_entry">True</property>
    <property name="entry_text_column">0</property>
    <signal name="changed" handler="on_combobox_license_changed" swapped="no"/>
    <child>
      <object class="GtkCellRendererText" id="cellrenderertext_license"/>
    </child>
    <child internal-child="entry">
      <object class="GtkEntry" id="combobox-entry2">
        <property name="can_focus">False</property>
        <property name="buffer">entrybuffer1</property>
      </object>
    </child>
  </object>

I created a liststore with one column of type gchararray containing the text. 我创建了一个列表存储,其中有一列类型为gchararray的文本包含文本。 The cell is rendered by the GtkCellRenderer (but the property "text" of the cellrenderer is not defined, because if I define it to 0 (the gchararray), I get the text twice !) 单元格由GtkCellRenderer渲染(但是未定义cellrenderer的属性“文本”,因为如果我将其定义为0(gchararray),则会两次获得文本!)

I thought adding an entrybuffer would help, but it does not change anything. 我以为添加一个入口缓冲区会有所帮助,但不会改变任何东西。

EDIT : I found the solution : can_focus was false for the embedded entry. 编辑:我发现解决方案:can_focus对于嵌入式条目为false。 Now it works, without the need to an entrybuffer. 现在它可以工作了,而无需进入缓冲区。

I found the solution before posting this, but I post it in case other users have this issue too. 我在发布此内容之前找到了解决方案,但我也发布了它,以防其他用户也遇到此问题。

将组合框的嵌入式条目的can_focus属性更改为true。

If you prefer to use the get_active_text() methods, then you just need to create a Gtk.ComboBoxText instead of a Gtk.ComboBox . 如果您更喜欢使用get_active_text()方法, Gtk.ComboBoxText需要创建一个Gtk.ComboBoxText而不是Gtk.ComboBox This API was split off into another class in GTK 3. 该API在GTK 3中被拆分为另一个类。

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

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