python - 尝试在 KivyMD 中删除小部件时出错
问题描述
我正在尝试用 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()
解决方案
利用:
def remove_the_test_ID_widget(self):
self.General.ids.BL.remove_widget(self.General.ids.test)
在您的示例中,根据您需要在何处或何时删除“测试”小部件的功能,我将其放在加号图标中,因此当您按下它时,它将删除“测试”:
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: 'pencil'
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: 'pencil'
pos_hint: {"center_x": .30, "center_y": .15}
user_font_size: '35dp'
on_release:app.Games("one")
MDIconButton:
id:two
icon: 'plus'
pos_hint: {"center_x": .45, "center_y": .2}
user_font_size: '35dp'
on_release:app.Games("two"),app.remove_the_test_ID_widget()
MDIconButton:
id:three
icon: 'home'
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 Gamejbsidis(MDApp):
def build(self):
self.General = Builder.load_string(KV)
return self.General
def remove_the_test_ID_widget(self):
self.General.ids.BL.remove_widget(self.General.ids.test)
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'
Gamejbsidis().run()
推荐阅读
- python - 在打印语句之前执行的 Jupyter Notebook 输入行
- python - SymPy 表达式的 `evalf` 方法的 `subs` 参数究竟是什么?像`s.evalf(subs={...})`?
- spring-kafka - spring kafka 1.2.2 优雅关闭
- java - 如何更新主从流活动的 listFragment
- django - 查看不返回 HttpResponse
- javascript - 承诺结束的等待功能
- android - 反应本机 onesignal android 推送令牌为空
- yii2 - Codeception:canSeeRecord() 和 seeRecord() 有什么区别
- javascript - Isotope & javascript - 添加新元素后元素不再可点击
- firebase - Flutter Firebase 身份验证 currentUser() 返回 null