我正在尝试在我的对象游戏中实现一个计时器,但它经常给我:
File "/home/nabeel/Desktop/game/ProjectGame/main.py", line 71, in call
self.time_number = str(22)
AttributeError: 'float' object has no attribute 'time_number'
我无法理解为什么会发生这种情况?
.py文件
__version__ = "1.0"
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.image import Image
from kivy.config import Config
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.graphics.instructions import InstructionGroup
from kivy.graphics.context_instructions import Color
import random
Config.set('graphics', 'width', '480')
Config.set('graphics', 'height', '320')
ImagesList = ['calculator','cards','facebook','fly','glasses','keys','mouse','wallet','webcam','pen']
running = True
class ImgButton(Button):
pass
class ListLabel(Label):
pass
class MyWidget(AnchorLayout):
level = 1
LOC = []
for i in range (10):
LOC.append((random.randint(50,400),random.randint(0,260)))
num = 9
b_G_IMG = StringProperty("Level1/B_image1.png")
time = 40
time_number = StringProperty("")
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
self.time_number = str(self.time)
def remove_rectangle(self, widget):
if self.num > 0:
self.grid_layout.remove_widget(widget)
self.num -= 1
else:
self.level += 1
self.set_level(self.level)
self.num = 9
for i in range(9):
new_image = ImgButton(background_normal= "Level1/"+str(ImagesList[i])+".png", pos = self.LOC[i])
self.grid_layout.add_widget(new_image)
new_image.bind(on_press=self.remove_rectangle)
new_label = ListLabel(text=ImagesList[i],pos_y=(120+(i*10)))
self.grid_layout.add_widget(new_label)
def set_level(self,level):
self.b_G_IMG = "Level"+str(level)+"/B_image"+str(level)+".png"
def call(self):
self.time = self.time + 1
self.time_number = str(self.time)
Clock.schedule_interval(call, 1)
def ticked(self):
self.time_number = 50
running=False
class WidgetsApp(App):
def build(self):
return MyWidget()
if __name__=="__main__":
WidgetsApp().run()
.kv文件
<ListLabel@Label>
halign:'left'
font_size:15
markup:True
<ImgButton@Button>:
size_hint:(None,None)
size:(60,60)
<MyWidget>:
grid_layout: grid_layout
AnchorLayout:
BoxLayout:
Image:
source:root.b_G_IMG
FloatLayout:
Label:
pos:(-200,120)
font_size: 33
text:':50'
FloatLayout:
ListLabel:
pos:(-200,20)
text:'[color=000000]Calculator[/color]'
ListLabel:
pos:(-200,10)
text:'[color=000000]Cards[/color]'
ListLabel:
pos:(-200,0)
text:'[color=000000]facebook[/color]'
ListLabel:
pos:(-200,-10)
text:'[color=000000]fly[/color]'
ListLabel:
pos:(-200,-20)
text:'[color=000000]glasses[/color]'
ListLabel:
pos:(-200,-30)
text:'[color=000000]keys[/color]'
ListLabel:
pos:(-200,-40)
text:'[color=000000]mouse[/color]'
ListLabel:
pos:(-200,-50)
text:'[color=000000]wallet[/color]'
ListLabel:
pos:(-200,-60)
text:'[color=000000]webcam[/color]'
ListLabel:
pos:(-200,-70)
text:'[color=000000]Pen[/color]'
FloatLayout:
size:(480,320)
size_hint:(480,320)
id: grid_layout
ImgButton:
pos:(root.LOC[0])
background_normal: 'Level1/pen.png'
on_press: root.remove_rectangle(widget=self)
ImgButton:
pos:(root.LOC[1])
background_normal: 'Level1/cards.png'
on_press: root.remove_rectangle(widget=self)
ImgButton:
pos:(root.LOC[2])
background_normal: 'Level1/calculator.png'
on_press: root.remove_rectangle(widget=self)
ImgButton:
pos:(root.LOC[3])
background_normal: 'Level1/fly.png'
on_press: root.remove_rectangle(widget=self)
ImgButton:
pos:(root.LOC[4])
background_normal: 'Level1/glasses.png'
on_press: root.remove_rectangle(widget=self)
ImgButton:
pos:(root.LOC[5])
background_normal: 'Level1/keys.png'
on_press: root.remove_rectangle(widget=self)
ImgButton:
pos:(root.LOC[6])
background_normal: 'Level1/mouse.png'
on_press: root.remove_rectangle(widget=self)
ImgButton:
pos:(root.LOC[7])
background_normal: 'Level1/wallet.png'
on_press: root.remove_rectangle(widget=self)
ImgButton:
pos:(root.LOC[8])
background_normal: 'Level1/webcam.png'
on_press: root.remove_rectangle(widget=self)
ImgButton:
pos:(root.LOC[9])
background_normal: 'Level1/matches.png'
on_press: root.remove_rectangle(widget=self)