有10个滑块,及每个滑块值改变在窗口改变图像。但我不能找到一个方法来做到这一点没有变化()函数调用root.mainloop()。此原因虽然最终堆栈溢出和我检查其通过印刷回溯长度(“存储器”变量)。
root = tk.Tk()
class SliderClass:
def __init__(self,i,j):
global no
self.no = no
self.w = Scale(root, label="PCA_feature "+str(self.no+1),
from_=10, to=-10, tickinterval=0.1, orient=HORIZONTAL, showvalue=0)
self.w.grid(row=i,column=j)
self.w.bind("<ButtonRelease-1>", self.change)
self.w.set(np.clip(z[self.no],-10.0,10.0))
no +=1
def change(self, event):
memory = ''.join(traceback.format_stack())
print(len(memory))
z[self.no] = self.w.get()
z_inv = pca.inverse_transform(z).reshape((1,-1))
im = G.layers[2].predict(z_inv)
im = (0.5 * im + 0.5)*255
im = im[0,:,:,:].astype('uint8')
im = cv2.resize(im,(150,150))
im = Image.fromarray(im)
im = PhotoImage(im)
panel.configure(image=im)
root.mainloop()
im = Image.fromarray(im)
im = PhotoImage(im)
panel = Label(root, image = im, width=300,height=300)
panel.grid(rowspan=2,column=0)
r,c = 2,5
for i in range(r):
for j in range(1,c+1):
s = SliderClass(i,j)
sliders.append(s)
root.mainloop()
你不需要在你的函数调用mainloop
。它应该为你的程序的生命被调用一次。
当您删除调用mainloop
图像不显示的事实是,因为你没有存储到图像的引用,这样会将它被垃圾收集器在功能超出范围中删除。通过在功能运行mainloop
你避免这种情况发生,但会导致更糟糕的问题(即一个你写)
除了去除调用你的函数里面mainloop
,您需要保存的图像对象的引用。一个简单的变化是使用属性喜欢self.im
而不是局部变量im
。