2017-10-04 112 views
-1

我希望我的代码能够将显示的比萨饼预览更改为任意披萨,但我无法弄清楚如何对其进行描述。 现在(我放大,因为我使用的小图像)从列表中更改图片tkinter

from tkinter import * 
from itertools import cycle 

class buttonInitialize: 
    def __init__ (self,enabler): 
     leftframe=Frame(enabler) 
     leftframe.pack(side=LEFT) 
     rightframe=Frame(enabler) 
     rightframe.pack(side=RIGHT) 

     pizzaList=[] 
     pizzaList.append(PhotoImage(file="0.png").zoom(10)) 
     pizzaList.append(PhotoImage(file="1.png").zoom(10)) 


     self.otherButton=Button(leftframe,text="??",command=self.makePizza(1)) 
     self.otherButton.pack() 

     self.printButton=Button(rightframe,image=pizzaList[0]) 
     self.printButton.pack() 

    def makePizza(self,index): 
     self.printButton["image"]=self.pizzaList[index] 
root=Tk() 
c=buttonInitialize(root) 
root.mainloop() 

,我得到了错误

AttributeError: 'buttonInitialize' object has no attribute 'pizzaList' 

谢谢!

+1

'pizzaList'不同于'self.pizzaList'。 – Lafexlos

+0

我试过这两种方式,它仍然会抛出相同的错误。你知道我如何更新图像吗? –

+0

添加了答案。希望,这清楚了一点。 – Lafexlos

回答

1

self.pizzaList - >类变量
pizzaList - >局部变量

要访问pizzaListmakePizza,你应该使用self.前缀__init__创建时做出pizzaList该类的变量。

class buttonInitialize: 
    def __init__ (self,enabler): 
     ... 
     ... 
     self.pizzaList= [] 
     self.pizzaList.append(PhotoImage(file="0.png").zoom(10)) 
+0

谢谢!这是问题的固定部分。我现在得到错误AttributeError:'buttonInitialize'对象没有属性'printButton' –

+0

@I_Queerly_Belong_Here您在上面提供的代码不应该抛出该错误。在某个地方肯定有一个错字。无论如何,你的_new_错误都有相同的来源。 – Lafexlos

+0

哦,我发现这个问题,我只是用intertools语法更新面板,而不是原始的Tkinter语法。 –