Python无法在类中分配值
我无法在类中传递/分配值。 这是我的班级定义,基本上我想根据时间(一天中的时间)更改颜色。Python无法在类中分配值
class Meter(tk.Canvas): def __init__(self,master,*args,**kwargs):
super(Meter,self).__init__(master,*args,**kwargs)
self.tod=tod
self.layoutparams(tod)
self.graphics()
self.createhand()
self.setrange()
def layoutparams(self,tod):
# set parameters that control the layout
height = int(self['height'])
width = int(self['width'])
# find a square that fits in the window
if(height*2 > width):
side = width
else:
side = height*2
# set axis for hand
self.centrex = side/2
self.centrey = side/2
# standard with of lines
self.linewidth = 2
# outer radius for dial
self.radius = int(0.40*float(side))
# set width of bezel
self.bezel = self.radius/15
self.bezelcolour1 = 'green'
here is where i change the colour
if (tod=='day'):
self.bezelcolour2 = 'black'
else:
self.bezelcolour2 = 'white'
self.bezelcolour3 = 'red'
# set lengths of ticks and hand
self.majortick = self.radius/8
self.minortick = self.majortick/2
这里是我该怎么办实例
w=Meter(self,height = 400,width = 400) w.tod='day'
我收到此错误
line 27, in __init__ self.tod=tod
NameError: name 'tod' is not defined
有什么不对?
回答:
问题正是错误告诉你的。您将tod
的值指定为self.tod
,但您没有名为tod
的变量。
回答:
你必须写:
w=Meter(self,height = 400,width = 400, tod='day')
,这将是更好的写:
if (self.tod=='day'):
你或许应该还添加了一个功能changeTod
。
以上是 Python无法在类中分配值 的全部内容, 来源链接: utcz.com/qa/265633.html