问题
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'
What am I doing wrong?
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()
回答1:
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.
来源:https://stackoverflow.com/questions/40939934/trace-variable-combobox-tkinter