gpt4 book ai didi

python - Python不断显示 'object has no such attribute'

转载 作者:行者123 更新时间:2023-12-02 10:57:42 25 4
gpt4 key购买 nike

由于某些原因,执行以下代码行时,会出现错误说明:'car'对象没有属性'time_int'该类称为car。

尽管我将方法包括在同一个类中,但是每次尝试调用self.time_int时,上面都会出现错误,这是代码片段:

# the __init__ also has the buttons that connect to other methods, but I just included the relevant one

class car:


def __init__(self,master):

self.helv = tkinter.font.Font(family ="Helvetica", size = 20, weight = 'bold')

self.master = master

master.title('RC car controls')
self.background_image = PhotoImage(file='1.gif') #upload background picture
self.background_label = Label(master, image=self.background_image)
self.background_label.image = self.background_image
self.background_label.place(x=0,y=0,relwidth=1, relheight=1) #placing of background picture
master.geometry("500x500")
master.resizable(0, 0)

self.acl = Button(master, command = lambda: [self.set_down(),self.set_up()], image = self.img,
padx = 4, pady = 4)
self.acl.place(relx = 0.7, rely = 0.5)


def set_down(self):
self.acl.bind('<Button-1>', self.accel_gn)


def set_up(self):
self.acl.bind('<ButtonRelease-1>',self.accel_fn)

def accel_fn(self,accel_fn): ##### ACCELERATION SLOWS DOWN
self.start = time()
print('up')
self.time_int = (self.start-self.end)
print(self.time_int)

def accel_gn(self,accel_gn): ##### ACCELERATION INCREASES
self.end = time()
a = speed()
print('down')
if 1 > self.time_int > 0:
print('current motor speed:', a.final_speed())


错误在if语句行的第二种方法 accel_gn(self,accel_gn)中发生。为了明确起见,该按钮基本上检查了按下按钮的时间,并在另一个名为final_speed()的类中调用一个方法。

谢谢

编辑:当我第二次在空闲状态下按下按钮时,它将继续运行,并且错误不会停止它。仅在第一次单击按钮时 pop 错误。

最佳答案

我认为您需要在self.time_int中定义accel_gn

关于python - Python不断显示 'object has no such attribute',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56606081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com