跟踪变量-ComboBox Tkinter [英] Trace Variable - ComboBox Tkinter

查看:41
本文介绍了跟踪变量-ComboBox Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ComboBox 小部件跟踪变量.更改 ComboBox 值时,出现以下错误:

I'm trying to trace a variable using a ComboBox Widget. When I change the ComboBox value I get the following error:

AttributeError: 'StringVar' object has no attribute '_report_exception'

我在做什么错了?

import tkinter as tk
from tkinter import ttk, StringVar

class TEST(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent

        self.estvalue = StringVar()

        self.pestanas = ttk.Notebook(self.parent)
        self.geometria = ttk.Frame(self.pestanas)
        self.viento = ttk.Frame(self.pestanas)
        self.topografia = ttk.Frame(self.pestanas)
        self.pestanas.add(self.geometria, text="Parámetros Globales")
        self.pestanas.add(self.viento, text="Viento")
        self.pestanas.grid(sticky=tk.W, pady=6, padx=6)
        Estructura = ["Edificios",
                      "Chimeneas, Tanques y Estructuras similares",
                      "Carteles llenos",
                      "Carteles abiertos y Estructuras similares"]
        self.Estructura = tk.LabelFrame(self.geometria, text="Estructura",
                                        labelanchor="nw", borderwidth="2",
                                        relief="groove", pady=5, padx=5)
        self.Estructura.grid(column=0, row=0, sticky=tk.W)
        self.Est = ttk.Label(self.Estructura, text="Tipo de Estructura")
        self.Est.grid(column=0, row=0, sticky=tk.W)
        self.Est = ttk.Label(self.Estructura, text="Tipo de Estructura")
        self.Est.grid(column=0, row=1, sticky=tk.W)
        self.boxest = ttk.Combobox(self.Estructura, textvariable=self.estvalue,
                                   state='readonly', width=36)
        self.boxest['values'] = Estructura
        self.boxest.current(0)
        self.boxest.grid(column=1, row=0, sticky=tk.E, padx=5)

        self.estvalue.trace_variable("w",self.eventest())

    def eventest(self):
        if self.estvalue.get() == "Edificios":
            print("foo")
        else:
            print("bar")

def main():
    root = tk.Tk()
    app = TEST(root)
    app.grid()
    root.title("App")
    root.focus_force()
    root.minsize(width=600, height=390)
    root.columnconfigure(0, weight=1)
    root.mainloop()

if __name__ == '__main__':
    main()

推荐答案

您必须为函数提供跟踪引用.相反,您是调用函数,并在函数返回的任何位置进行跟踪.

You must give trace a reference to a function. Instead, you're calling the function and giving trace whatever the function returns.

像这样设置跟踪(注意缺少的())

Set up the trace like this (note the missing ())

self.estvalue.trace_variable("w",self.eventest)

跟踪会将三个值传递给函数,因此即使您不使用这些参数,也需要修改函数以接受这些参数.

The trace will pass along three values to the function, so you'll need to modify your function to accept those arguments even if you don't use them.

这篇关于跟踪变量-ComboBox Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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