如何动态更新 ttk.combobox? [英] How can I dynamically update ttk.combobox?

查看:46
本文介绍了如何动态更新 ttk.combobox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Windows 8 计算机上使用 Python 3.4 和 Tkinter 创建 GUI.

I am creating a GUI using Python 3.4 and Tkinter on a Windows 8 computer.

GUI 顶部有一些 Entry 输入,然后是一些组合框.我希望组合框从由先前输入(文件名、找到必要信息的行、分隔符类型等)描述的文本文件中获取选项列表.我正在尝试使用 postcommand,但它似乎首先运行并且从不更新,而不是每次访问组合框的下拉菜单时更新.

The GUI has some Entry inputs at the top, then some comboboxes. I want the combobox to acquire a list of options from a text file that is described by the previous inputs (file name, the row in which the requisite information is found, delimiter type, etc.). I am trying to use the postcommand, but it seems to run first thing and never update, rather then update every time I access the drop down menu of the combobox.

    datatypes = []
    datatypes = ttk.Combobox(tab_loc, textvariable=std1, values=datatypes, postcommand=self.get_datatypes(datatypes,
           self.flnm2.get(), self.hl2_text.get(), self.delim2.get(), self.fcd2_text.get())).grid(pady=v_pad,
           padx=h_pad, row=8, column=1, sticky=EW)

    def get_datatypes(self, lst, flnm, hl, delim, fcd):
        # Problem: postcommand runs at start of GUI. Prevents updating.
        lst += ["test", "worked?"]
        print("stuff")
        lst += flnm
        lst += hl
        try:
            # open the file, get the line, break it apart.
        except:
            pass

self.flnm2self.hl2_textself.delim2self.fcd2_text 是其中的一些之前的输入.它们是 StringVar.

self.flnm2, self.hl2_text, self.delim2, and self.fcd2_text are some of the previous inputs. They are StringVar.

当我运行这段代码时,组合框有选项 test、working? 和两个空行(大概用于 flnmhl).我计划有多个像这样的组合框,只是有不同的输入,所以我需要一个函数,我可以提供输入然后更新 datatypes.

When I run this code, the combobox has the options test, worked?, and two blank lines (presumably for flnm and hl). I plan to have multiple comboboxes like this, just with different inputs, so I need a function I can give inputs to and then updates datatypes.

我做错了什么?

推荐答案

您正在调用 self.get_datatypes(...) 并将结果分配给 postcommand 属性在您创建组合框时.这就是为什么它只运行一次:你告诉它运行.就像使用 command 属性一样,您必须在定义 postcommand 属性时给出对函数的引用.

You are calling self.get_datatypes(...) and assigning the result to the postcommand attribute at the time you create the combobox. That is why it runs exactly once: you told it to. Just like with command attributes, you must give a reference to a function when definining the postcommand attribute.

为每个组合框的 post 命令创建一个专门的方法,为您的 postcommand 使用该方法的引用,然后从该函数中获取值后调用 get_datatypes其他小部件.

Create a method specifically for the post command for each combobox, use a reference to that for your postcommand, and then call get_datatypes from that function after fetching the values from the other widgets.

它应该看起来像这样:

datatypes = ttk.Combobox(..., postcommand=self.combo_post_command, ...)
...
def combo_post_command(self):
    flnm2 = self.flnm2.get()
    hl2_text = self.hl2_text.get()
    delim2 = self.delim2.get()
    fcd2_text = self.fcd2_text.get()
    return self.get_datatypes(datatypes, flnm2, hl2_text, delim2, fcd2_text)

我不确定 datatypes 应该是什么.您将其定义为一个空列表,然后将其重置为小部件本身.无论如何,这显示了一般概念.

I'm not exactly sure what datatypes is supposed to be. You define it as an empty list, then reset it to be the widget itself. Regardless, this shows the general concept.

通过为每个组合框设置一个函数,您似乎有很多重复的代码,但您必须在某处调用所有 get() 函数.您要么尝试将所有内容都塞入小部件的配置中,要么将其放入一个函数中.将其放在函数中更加明确,并且随着时间的推移更易于调试和维护.

It may seem like you have a lot of duplicated code by having a function for each combobox, but you have to call all the get() functions somewhere. You either try to cram that all into the configuration of the widget, or you put it in a function. Putting it in the function is more explicit, and easier to debug and maintain over time.

这篇关于如何动态更新 ttk.combobox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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