Python 如何在编写时更新组合框值?

编程入门 行业动态 更新时间:2024-10-27 02:24:44
本文介绍了Python 如何在编写时更新组合框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:13:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   如何在   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!