跟踪变量 - ComboBox Tkinter

时间:2016-12-02 19:56:00

标签: python python-3.x tkinter combobox

我尝试使用ComboBox小部件跟踪变量。当我更改ComboBox值时,我收到以下错误:

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()

0 个答案:

没有答案