Python 2.7 更新kivy中的文本
我在kivy手下做一个“隐藏对象游戏”。我已经实现了所讨论的方法,到目前为止,对象会被绘制出来,一旦单击就会被删除。现在,我正在寻找一种在屏幕上显示其名称的方法,一旦触摸到对象,对象和相应的标签都会被删除 我如何做到这一点 我尝试添加标签,然后使用与图像相同的方法删除标签,但不起作用 所以我的第二个猜测是把名字写在GIMP的背景上,然后使用这些图片。这种方法好吗,还是我在监督更简单的方法 .py文件 .kv文件Python 2.7 更新kivy中的文本,python-2.7,kivy,Python 2.7,Kivy,我在kivy手下做一个“隐藏对象游戏”。我已经实现了所讨论的方法,到目前为止,对象会被绘制出来,一旦单击就会被删除。现在,我正在寻找一种在屏幕上显示其名称的方法,一旦触摸到对象,对象和相应的标签都会被删除 我如何做到这一点 我尝试添加标签,然后使用与图像相同的方法删除标签,但不起作用 所以我的第二个猜测是把名字写在GIMP的背景上,然后使用这些图片。这种方法好吗,还是我在监督更简单的方法 .py文件 .kv文件 哈利格:“左” 字体大小:15 标记:正确 : 大小提示:(无,无) 尺寸:(60
哈利格:“左”
字体大小:15
标记:正确
:
大小提示:(无,无)
尺寸:(60,60)
:
网格布局:网格布局
主持人安排:
盒子布局:
图片:
来源:root.b_G_IMG
浮动布局:
标签:
位置:(-200120)
字体大小:33
文本:root.time\u编号
浮动布局:
列表标签:
位置:(-200,20)
文本:'[color=000000]计算器[/color]'
列表标签:
位置:(-200,10)
文本:'[color=000000]卡片[/color]'
列表标签:
位置:(-200,0)
文本:“[color=000000]facebook[/color]”
列表标签:
位置:(-200,-10)
文本:'[color=000000]fly[/color]'
列表标签:
位置:(-200,-20)
文本:'[color=000000]眼镜[/color]'
列表标签:
位置:(-200,-30)
文本:'[color=000000]键[/color]'
列表标签:
位置:(-200,-40)
文本:'[color=000000]鼠标[/color]'
列表标签:
位置:(-200,-50)
文本:“[color=000000]钱包[/color]”
列表标签:
位置:(-200,-60)
文本:'[color=000000]网络摄像头[/color]'
列表标签:
位置:(-200,-70)
文本:'[color=000000]笔[/color]'
浮动布局:
尺寸:(480320)
尺寸提示:(480320)
id:网格布局
img按钮:
位置:(根位置[0])
背景_正常:“Level1/pen.png”
按:根。删除矩形(widget=self)
img按钮:
位置:(根位置[1])
背景_正常:“Level1/cards.png”
按:根。删除矩形(widget=self)
img按钮:
位置:(根位置[2])
背景_正常:“Level1/calculator.png”
按:根。删除矩形(widget=self)
img按钮:
位置:(根位置[3])
背景_正常:“Level1/fly.png”
按:根。删除矩形(widget=self)
img按钮:
位置:(根位置[4])
背景_正常:“Level1/glasses.png”
按:根。删除矩形(widget=self)
img按钮:
位置:(根位置[5])
背景_正常:“Level1/keys.png”
按:根。删除矩形(widget=self)
img按钮:
位置:(根位置[6])
背景_正常:“Level1/mouse.png”
按:根。删除矩形(widget=self)
img按钮:
位置:(根位置[7])
背景_正常:“Level1/wallet.png”
按:根。删除矩形(widget=self)
img按钮:
位置:(根位置[8])
背景:正常:“Level1/webcam.png”
按:根。删除矩形(widget=self)
img按钮:
位置:(根位置[9])
背景_正常:“Level1/matches.png”
按:根。删除矩形(widget=self)
如果有人想看一看,整个代码和它的资产都在这里
寻找并提供建议
您是否也可以使用相同的ImgButton类来创建标签?然后可以使用完全相同的删除方法?主要问题是将图像与其标签相关联,因此一旦图像被删除,文本也会被删除
__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_number = StringProperty("")
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
self.game_start()
Clock.schedule_interval(self.call, 1)
def time_dec(self):
if self. time > 0:
self.time = self.time - 1
self.time_number = str(self.time)
else:
self.running = False
def game_start(self):
self.time = 5
self.time_number = str(self.time)
self.level = 1
self.running = True
def remove_rectangle(self, widget):
if self.running:
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)
else:
self.grid_layout.remove_widget(widget)
self.running = True
self.game_start()
def set_level(self,level):
self.b_G_IMG = "Level"+str(level)+"/B_image"+str(level)+".png"
def call(self,dt):
if self.running:
self.time_dec()
else:
game_image = Button(background_normal= "GameOver.png", pos = (40,40))
self.grid_layout.add_widget(game_image)
game_image.bind(on_press=self.remove_rectangle)
class WidgetsApp(App):
def build(self):
return MyWidget()
if __name__=="__main__":
WidgetsApp().run()
<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:root.time_number
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)