问题:IntVar无法更新
我正在尝试创建一个窗口,显示3个框架,带9个检查按钮,每帧选择1个检查按钮,以更改图像可视化的颜色图,这是我正在键入的大代码的一小部分。 这是我改变色带的代码:
class Inicio(tk.Tk):
def __init__(self, *args, **kwargs):
...
#Cambiar_banda_de_colores
def cambio_banda(self):
cambio = tk.Tk()
cambio.wm_title("Cambio de bandas del Landsat")
top_frame = tk.Frame(cambio, bg='white', bd=2, relief=tk.SUNKEN)
top_frame.pack(side=tk.TOP ,fill="both", expand=True)
ttk.Label(top_frame, text="Escoja 3 bandas para visualizacion en orden (RGB) ").pack(side=tk.TOP )
#Frame 1
box1 = tk.LabelFrame(top_frame, text="Primera Banda ", bg='red', width=110, height= 250, bd=3, relief=tk.RAISED)
box1.pack(side=tk.LEFT,fill="both")
#Frame 2
box2 = tk.LabelFrame(top_frame, text="Segunda Banda ", bg='green', width=110, height= 250, bd=3, relief=tk.RAISED)
box2.pack(side=tk.LEFT,fill="both")
#Frame 3
box3 = tk.LabelFrame(top_frame, text="Tercera Banda ", bg='blue', width=110, height= 250, bd=3, relief=tk.RAISED)
box3.pack(side=tk.LEFT,fill="both")
#my 3 IntVar
ssbanda1 = tk.IntVar()
ssbanda2 = tk.IntVar()
ssbanda3 = tk.IntVar()
#an inside funtion just to print
def cambiar():
print "Bandas seleccionadas"
print "banda1: ", ssbanda1.get(), ", banda2: ", self.ssbanda2.get(), ", banda3: ", self.ssbanda3.get()
#To optimize code I am creating the checkbutton like this:
lista = range(1, 10, 1)
lista2 = ['Banda Costera', 'Banda Azul', 'Banda Verde', 'Banda Rojo', 'Banda NIR', 'Banda SWIR 1',
'Banda SWIR 2', 'Banda Pancromatica', 'Banda Cirrus']
for l1, l2 in zip(lista, lista2, ):
a = tk.Checkbutton(box1, text="{0} ({1})".format(l2, l1), onvalue=l1, variable=ssbanda1)
a.pack(side=tk.TOP, fill=tk.X)
#repeat 2 more times for `ssbanda2` and `ssbanda3`
#button to call `cambiar` func to print
boton1 = tk.Button(top_frame, text="Aceptar", command= cambiar)
boton1.pack(side=tk.BOTTOM, fill=tk.X)
cambio.mainloop()
但是当我打印时,我得到了ssbanda1=0
之前我创造了这样的漫长道路:
a1 = tk.Checkbutton(box1, anchor= "w", text="(1)Banda Costera", variable=ssbanda1, command= lambda: ssbanda1.set(1), onvalue=1).pack(side=tk.TOP, fill=tk.X)
a2 = tk.Checkbutton(box1, anchor= "w",text="(2)Banda Azul", variable=ssbanda1, command= lambda: ssbanda1.set(2), onvalue=2).pack(side=tk.TOP, fill=tk.X)
a3 = tk.Checkbutton(box1, anchor= "w",text="(3)Banda Verde", variable=ssbanda1, command= lambda: ssbanda1.set(3), onvalue=3).pack(side=tk.TOP, fill=tk.X)
a4 = tk.Checkbutton(box1, anchor= "w",text="(4)Banda Rojo", variable=ssbanda1, command= lambda: ssbanda1.set(4), onvalue=4).pack(side=tk.TOP, fill=tk.X)
a5 = tk.Checkbutton(box1, anchor= "w",text="(5)Banda NIR", variable=ssbanda1, command= lambda: ssbanda1.set(5), onvalue=5).pack(side=tk.TOP, fill=tk.X)
a6 = tk.Checkbutton(box1, anchor= "w", text="(6)Banda SWIR 1", variable=ssbanda1, command= lambda: ssbanda1.set(6), onvalue=6).pack(side=tk.TOP, fill=tk.X)
a7 = tk.Checkbutton(box1, anchor= "w",text="(7)Banda SWIR 2", variable=ssbanda1, command= lambda: ssbanda1.set(7), onvalue=7).pack(side=tk.TOP, fill=tk.X)
a8 = tk.Checkbutton(box1, anchor= "w",text="(8)Banda Pancromatica", variable=ssbanda1, command= lambda: ssbanda1.set(8), onvalue=8).pack(side=tk.TOP, fill=tk.X)
a9 = tk.Checkbutton(box1, anchor= "w",text="(9)Banda Cirrus", variable=ssbanda1, command= lambda: ssbanda1.set(9), onvalue=9).pack(side=tk.TOP, fill=tk.X)
但无法使用lambda
执行此操作,因为它会在循环中每次ssbanda1
覆盖并始终打印ssbanda1=9
尝试在单独的脚本中运行循环并且工作正常,但在我的程序中没有检查过this和this。
我必须使用self
表达式吗? global
个变量?在外面使用cambiar
函数?
这是窗口:
TL; DR:我正在尝试将9行优化为1个循环,但IntVar不会在按钮的回调函数中更新
答案 0 :(得分:2)
问题在于您创建的Tk
实例不止一次。每个都有自己的私有命名空间,其中一个小部件和变量不能被另一个共享。
如果您需要多个窗口,请创建Toplevel
的实例,而不是Tk
的多个实例。
这可能不是唯一的问题,但是这是一个很大的问题,在解决之前没有必要进一步调查。