简体   繁体   English

在 Python-GTK3 中设置条目文本的 set_text 在哪里?

[英]Where is set_text to set an entry text in Python-GTK3?

In previous pyGTK you could use combobox.set_title(text) to set the text entered into a combobox.在之前的 pyGTK 中,您可以使用 combobox.set_title(text) 来设置输入到组合框中的文本。 Now in Python-GTK3 it doesn't seem to work and the example does not include setting it.现在在 Python-GTK3 中它似乎不起作用,并且示例不包括设置它。 Is there a new method to setting the selection in this case?在这种情况下是否有设置选择的新方法?

As @Rawing commented, the question is a little bit confused but the goal seems to be: set the text on a Gtk.ComboBox with entry.正如@Rawing 评论的那样,这个问题有点困惑,但目标似乎是:在 Gtk.ComboBox 上设置带有条目的文本。

pyGTK and Gtk+ 2 have the set_title method. pyGTK 和 Gtk+ 2 有set_title方法。 Gtk+ 3 also had up to version 3.10 but as commented, it has been deprecated. Gtk+ 3 也有高达 3.10 版,但正如评论中所说,它已被弃用。 Anyway the method did the same on both versions, and that was: " Sets the menu's title in tearoff mode ", which has nothing to do with the entry.无论如何,该方法在两个版本上都是一样的,那就是:“在撕下模式下设置菜单的标题”,这与条目无关。

The indicated example uses a Gtk.ComboxBox with model.指示的示例使用带有模型的 Gtk.ComboxBox。 To set the content of the entry we can set the active item from the model and the Gtk.Entry will reflect that by showing the text from that same item.要设置条目的内容,我们可以设置模型中的活动项目,Gtk.Entry 将通过显示来自同一项目的文本来反映这一点。

If you add name_combo.set_active (1) to the example code, like this:如果将name_combo.set_active (1)添加到示例代码中,如下所示:

...
name_combo = Gtk.ComboBox.new_with_model_and_entry(name_store)
name_combo.connect("changed", self.on_name_combo_changed)
name_combo.set_entry_text_column(1) 
name_combo.set_active (1) # ADD THIS LINE <--------------------
vbox.pack_start(name_combo, False, False, 0)
....

You will verify that the Gtk.Entry from the Gtk.ComboBox will be filled with text from the model.您将验证来自 Gtk.ComboBox 的 Gtk.Entry 将填充来自模型的文本。

If somehow you want to deal directly with the Gtk.Entry you can use Gtk.ComboBox inherited get_child method (from Gtk.Bin).如果您想以某种方式直接处理 Gtk.Entry,您可以使用 Gtk.ComboBox 继承的get_child方法(来自 Gtk.Bin)。 Then you can use Gtk.Entry set_text method, eg:然后你可以使用 Gtk.Entry set_text方法,例如:

name_combo = Gtk.ComboBox.new_with_model_and_entry(name_store)
name_combo.connect("changed", self.on_name_combo_changed)
name_combo.set_entry_text_column(1)
name_combo.get_child ().set_text ("Hello World") # <-------------- HERE
vbox.pack_start(name_combo, False, False, 0)

If you change the example as shown above, when you run it the Gtk.Entry will show the text as set.如果你改变上面的例子,当你运行它时 Gtk.Entry 将显示文本设置。

The text will not be in the model though.不过,文本不会出现在模型中。

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

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