Python 如何在编写时更新组合框值? [英] Python How to update combobox values while writing?

查看:31
本文介绍了Python 如何在编写时更新组合框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在编辑组合框上部文本字段上的文本时更新一个值列表,但我没有找到访问该文本字段值的选项.

I want to have a list of values that is updated when I edit the text on the upper text field of the ComboBox, but I have found no option to access the value of this text field.

我想出了一种荒谬的解决方案,包括在 ComboBox 标题上盖章的输入框,这样我就可以编辑输入框并绑定它以获取其值以更新 ComboBox.

I came up with a kind of ridiculous solution, consisting in an Entry Box stamped over the ComboBox header, so I can edit the Entry Box and bind it to get its value to update the ComboBox.

我怎样才能做得更好?

import tkinter as tk
from tkinter import ttk
def Run(size=(300,100)):
    a_set=('one','two','three')
    master = tk.Tk()
    master.geometry(str(size[0])+'x'+str(size[1]))
    lista=tk.ttk.Combobox(master,width=22,values=a_set)
    lista.grid(row=0,column=0)
    def update(event):
        a=event.widget.get()
        newvalues=[i for i in a_set if a in i]
        lista['values']=newvalues
    entry=tk.Entry(master)
    entry.bind('<KeyRelease>',update)
    entry.grid(row=0,column=0)
    
    master.mainloop()
Run()

推荐答案

您可以使用 combobox 的 textvariable 选项.

You can use the textvariable option of the combobox.

import tkinter as tk
from tkinter import ttk


def update(*args):

    newvalues=[i for i in a_set if var.get() in i]
    lista['values']=newvalues

    
a_set=('one','two','three')
master = tk.Tk()

var = tk.StringVar()
var.trace('w', update)

lista = ttk.Combobox(master, width=22, textvariable=var)
lista.grid(row=0,column=0)
   

master.mainloop()

这篇关于Python 如何在编写时更新组合框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆