i'm working on gui in python using tkinter. i'm reading text file in , creating gui elements dynamically based on lines in text file. have array each of element types, include labels, radiobutton variables (stringvars), , colored circles (drawn create_oval). goal when user changes radiobutton "not assigned" "in" or "out", colored circle on line change yellow green. here's how gui looks after text file has been read in:
item 1: (o) in () out () not assigned (g)
item 2: () in () out (o) not assigned (y)
currently, have trace on radiobutton stringvars can call method whenever 1 of buttons changed. problem figuring out radiobutton changed can change color of circle on line...
i'm going route of duplicating whole radiobutton stringvar array temp global array. when trace function called, compare temp array what's in array figure out change is. duplicate array with: temp_radiobutton_vars = list(radiobutton_vars), i'm not sure if right route. temp list , current list show same results when get() stringvar, after changed button. ideas on how fix this, or maybe there's better method i'm looking do...
sorry long , not great explanation. if needs more info or snippets of code, let me know. thanks!
there many ways solve problem. since using variable traces, perhaps simplest solution pass index of canvas item callback. can use lambda or functools.partial task. not use variable traces, instead, associate command each radiobutton. in both cases need tell callback index operate on.
in following example, callback takes reference variable , index canvas item. fetches value, looks color in table, , configures canvas item:
def on_radiobutton(var, index): value = var.get() color = {"in": "green", "out": "red", "unassigned": "yellow"} self.canvas.itemconfigure(index, fill=color[value]) this how trace set using lambda (note name1, name2 , op automatically sent tkinter every trace):
var = tk.stringvar() rb0 = tk.radiobutton(..., variable=var, value="in", text="in") rb1 = tk.radiobutton(..., variable=var, value="out", text="out") rb2 = tk.radiobutton(..., variable=var, value="unassigned", text="not assigned") var.trace("w", lambda name1, name2, op, index=i, var=var: on_radiobutton(var, index))
Comments
Post a Comment