我无法在课程中传递/分配值。 这是我的班级定义,基本上我想根据tod(时间)改变颜色。
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
有什么问题?
答案 0 :(得分:0)
问题正是错误告诉你的。您将MyModel._meta.get_all_related_objects()
[
f for f in MyModel._meta.get_fields()
if (f.one_to_many or f.one_to_one) and f.auto_created
]
的值分配给tod
,但您没有名为self.tod
的变量。
答案 1 :(得分:0)
你必须写:
w=Meter(self,height = 400,width = 400, tod='day')
最好写一下:
if (self.tod=='day'):
您可能还应该添加一个函数changeTod
。