python - 如何从entrycompletion获取entry

标签 python gtk3

我在看这篇文章:https://stackoverflow.com/a/2262200我正在编写的小东西中有一个非常相似的设置。我的问题是,一旦 entrycompletion 完成,并且输入框有 url,我如何从 completion 中获取该 url 到一个变量中? entry.get_text() 似乎不起作用,我尝试的其他一切似乎都给我一个对象或地址。如果您不想点击,这里是上面链接的引述。

# simplified example from the tutorial
import gtk

urls = [
    'http://www.google.com',
    'http://www.google.com/android',
    'http://www.greatstuff.com',
    'http://www.facebook.com',
    ]
liststore = gtk.ListStore(str)
for s in urls:
    liststore.append([s])

completion = gtk.EntryCompletion()
completion.set_model(liststore)
completion.set_text_column(0)

entry = gtk.Entry()
entry.set_completion(completion)

# boilerplate
window = gtk.Window()
window.add(entry)

window.connect('destroy', lambda w: gtk.main_quit())
window.show_all()
gtk.main()

最佳答案

您的代码的以下变体适用于我。

# simplified example from the tutorial
import gtk 

def on_match_selected(completion, treemodel, treeiter):
  print treemodel[treeiter][completion.get_text_column()]

urls = [ 
    'http://www.google.com',
    'http://www.google.com/android',
    'http://www.greatstuff.com',
    'http://www.facebook.com',
    ]   
liststore = gtk.ListStore(str)
for s in urls:
    liststore.append([s])

completion = gtk.EntryCompletion()
completion.set_model(liststore)
completion.set_text_column(0)

completion.connect('match-selected', on_match_selected)

entry = gtk.Entry()
entry.set_completion(completion)

# boilerplate
window = gtk.Window()
window.add(entry)

window.connect('destroy', lambda w: gtk.main_quit())
window.show_all()
gtk.main()

关于python - 如何从entrycompletion获取entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11073452/

相关文章:

go - 我对普通 GTK3 应用程序使用哪种语言重要吗?用户会注意到差异吗?

c - 使 GtkStatusbar 的文本可选择

python - 具有默认值的函数参数的 Pycharm 颜色突出显示

python - 如何在python中解码base64 url​​?

python - Python 中的正则表达式反向引用问题

c - GtkSourceView/GtkSourceBuffer - 如何用红色标记线条并显示图标

python - 使用scrapy抓取页面

python - 如何根据 pandas 中的最后一个用户事件添加行?

Gnome下的Java Swing应用——使用Adwaita(深色皮肤)窗口标题栏

linux - gstreamer 中是否有内置语音?