【发布时间】:2021-09-28 21:27:12
【问题描述】:
我正在尝试用python和kivymd编写最简单的应用程序,但遇到了错误。
line 172, in clears
self.General.ids.BL.remove_widget(self.test)
AttributeError: 'Game' object has no attribute 'test'
下面我将尝试只编写主要代码,为此我将删除不必要的细节,以免它们干扰感知。
问题 -> 无法删除小部件。 在 Python 代码中出现 MDIconButton 之后,它们需要在一段时间后被删除。 我认为对于代码中的 MDIconButton 元素,您需要通过 ids 添加一个 id,但到目前为止,甚至无法从已经具有 id 的 KV 标记中删除一个小部件。 我试图通过删除小部件
self.General.ids.BL.remove_widget(self.test)
但它给出了上面的错误。
理想情况下,我想弄清楚如何从 python 代码中删除小部件,但我将不胜感激。 如有必要,我准备展示整个代码。
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.animation import Animation
from kivymd.uix import screen
from kivymd.uix.screen import MDScreen
from kivymd.uix.button import MDIconButton
from kivymd.uix.label import MDIcon, MDLabel
from kivy.uix.widget import Widget
import random
import os
KV = '''
ScreenManager:
Screen:
FitImage:
source: 'path'
MDIconButton:
icon: 'path'
pos_hint: {"center_x": .5, "center_y": .70}
user_font_size: '35dp'
on_release:app.start()
Screen:
name: 'start_game'
FitImage:
source: 'path'
FloatLayout:
id : BL
orientation: 'vertical'
MDIconButton:
id:test
icon: 'path'
pos_hint: {"center_x": .30, "center_y": .15}
user_font_size: '35dp'
on_release:app.Games("one")
MDIconButton:
id:two
icon: 'path'
pos_hint: {"center_x": .45, "center_y": .2}
user_font_size: '35dp'
on_release:app.Games("two")
MDIconButton:
id:three
icon: 'path'
pos_hint: {"center_x": .60, "center_y": .15}
user_font_size: '35dp'
on_release:app.Games("three")
'''
#self.General.ids.BL.remove_widget(self.test)
class Game(MDApp):
def build(self):
self.General = Builder.load_string(KV)
return self.General
def hod_igrok(self, igrok, mix, miy,status=None):
status = '0'
if status == 'long':
igrok = igrok[0].replace(' ','')
else:
pass
print(igrok)
self.General.ids.BL.add_widget(
MDIconButton(
pos_hint={"center_x": mix, "center_y": miy},
icon=r"path %s.png"%igrok,
user_font_size= '50dp'
)
)
return self.General
def hod_comp(self, comp, c_x, c_y, status=None):
print(comp)
status = ' '
if status == 'long':
comp = comp[0].replace(' ','')
else:
pass
self.General.ids.BL.add_widget(
MDIconButton(
pos_hint={"center_x": c_x, "center_y":c_y},
icon= r"path c_%s.png"%comp,
user_font_size='50dp'
)
)
return self.General
def clears(self):
self.General.ids.BL.remove_widget(self.test)
def Games(self, igrok):
lis = ['kamen','nojnicy', 'bumaga']
comp= random.choice(lis)
if comp == "kamen" and igrok == "kamen" or comp == "nojnicy" and igrok == "nojnicy" or comp == "bumaga" and igrok == "bumaga":
print(comp,'/n',igrok)
print("Ничья")
self.hod_igrok(igrok, .45,.50, 'long')
self.clears()
elif comp == "kamen" and igrok == "bumaga" or comp == "nojnicy" and igrok == "bumaga" or comp == "bumaga" and igrok == "kamen":
print(comp,'/n',igrok)
print("Победил компьютор")
elif igrok == "bumaga" and comp == "kamen" or igrok == "nojnicy" and comp == "bumaga" or igrok == "kamen" and comp == "nojnicy":
print(comp,'/n',igrok)
print("Выиграл игрок")
def start(self):
self.root.current= 'start_game'
Game().run()
【问题讨论】:
-
请翻译标题。 Stack Overflow is an English-only site。不过,如果您愿意,也可以在 Stack Overflow на русском 上提问。
标签: python python-3.x kivymd