我正在尝试创建一个带有三个可点击项目的简单抽屉,其中第一个项目应该将我路由到另一个屏幕。 我不知道为什么会出现这个错误:
AttributeError: 'NoneType' object has no attribute 'update_items_color'
我在 kivy 或 kivymd 文档中没有找到任何帮助。 我有 kivy 版本 2.3.0、kivmd 版本 2.0.1.dev0 和 python 版本 3.12.3。有人可以帮我解决这个问题吗?我刚刚将 kivymd 更新到新版本,我正在尝试在此过程中学习 kivy 和 kivymd,但这并不容易。
这是我的.py 文件:
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivymd.uix.screen import MDScreen
from kivy.metrics import dp
from kivy.core.window import Window
from kivy.clock import Clock
import random as rd
Window.size = (360,640)
class MainApp(MDApp):
def build(self):
screen = Builder.load_file("myKv.kv")
self.theme_cls.primary_palette ="Orange"
self.theme_cls.primary_hue = "A700"
self.theme_cls.theme_style="Dark"
return screen
class EntryWindow(MDScreen):
#on_enter functions fire prior to the screen being set,
#therefore this is the proper way to do it
def on_enter(self):
Clock.schedule_once(self.go_home, 3)
print(self.manager.screens) # note screens is not yet set here...
def go_home(self, _):
print(self.manager.screens) # screens is set at this time
self.manager.current = "home"
class Home(MDScreen):
pass
class Gender(MDScreen):
def on_enter(self):
self.ids.extractedWord.color = (1,1,1,1)
f = open(".venv/genderWordDatabase.txt")
red = f.readlines()
stripped = [elem.strip() for elem in red]
separato = [elem.split() for elem in stripped]
dizWords = {}
for i in separato:
dizWords[i[1]] = (i[0],i[2])
print(dizWords)
extracted = list(dizWords.keys())[rd.randint(0,len(dizWords)-1)]
self.ids.extractedWord.font_style = "H6"
self.ids.extractedWord.text = extracted
def recordUpdate(self,extractedWord,operand):
f = open(".venv/genderWordDatabase.txt", "r")
red = f.readlines()
f.close()
for i in range(0,len(red)):
if extractedWord in red[i]:
red[i] = " ".join(red[i].strip("\n").split()[:-1]) + " " + str(int(red[i].strip("\n").split()[2])+operand)+"\n"
g = open(".venv/genderWordDatabase.txt", "w")
g.writelines(red)
def wordExtraction(self):
self.ids.extractedWord.color = (1,1,1,1)
f = open(".venv/genderWordDatabase.txt")
red = f.readlines()
f.close()
stripped = [elem.strip() for elem in red]
separato = [elem.split() for elem in stripped]
dizWords = {}
for i in separato:
dizWords[i[1]] = (i[0],i[2])
extracted = list(dizWords.keys())[rd.randint(0,len(dizWords)-1)]
self.ids.extractedWord.font_style = "Display"
self.ids.extractedWord.text = extracted
def check(self, answer):
f = open(".venv/genderWordDatabase.txt")
red = f.readlines()
f.close()
stripped = [elem.strip("\n").strip() for elem in red]
separato = [elem.split() for elem in stripped]
dizWords = {}
for i in separato:
dizWords[i[1]] = (i[0],i[2])
print(dizWords)
parola = self.ids.extractedWord.text
if dizWords[parola][0] == answer:
self.ids.extractedWord.color = (0,1,0,1)
self.ids.extractedWord.font_style = "Display"
self.ids.extractedWord.text = "Right +1"
#Chiamo la funzione che aggiorna il record di ciascuna parola
self.recordUpdate(dizWords[parola][0],1)
Clock.schedule_once(lambda dt: self.wordExtraction(), 1)
else:
self.ids.extractedWord.color = (1,0,0,1)
self.ids.extractedWord.font_style = "Display"
self.ids.extractedWord.text = "Wrong -1, the correct one was: " + dizWords[self.ids.extractedWord.text][0]
self.recordUpdate(dizWords[parola][0],-1)
Clock.schedule_once(lambda dt: self.wordExtraction(), 2)
MainApp().run()
这是我的 .kv 文件:
ScreenManager:
EntryWindow:
Home:
Gender:
<EntryWindow>:
name: "starting"
BoxLayout:
orientation: "vertical"
Widget:
MDLabel:
text: "German"
halign: "center"
markup: True
color: (1,1,1,1)
font_style: "Display"
MDLabel:
text: "By Staffo"
markup: True
color: (1,1,1,1)
halign: "center"
font_style: "Display"
Widget:
Widget:
<Home>:
name: "home"
MDNavigationLayout:
MDScreenManager:
MDScreen:
AnchorLayout:
anchor_x: "center"
anchor_y: "center"
MDBoxLayout:
orientation: "vertical"
padding: 10
spacing: 20
MDButton:
pos_hint: {"center_x": 0.1, "center_y":0.9}
on_release: nav_drawer.set_state("toggle")
MDIconButton:
icon: "menu"
style: "standard"
Widget:
MDCard:
padding: 10
MDRelativeLayout:
MDLabel:
text:"Trial"
MDCard:
padding: 10
MDRelativeLayout:
MDLabel:
text:"Trial"
MDCard:
padding: 10
MDRelativeLayout:
MDLabel:
text:"Trial"
Widget:
MDNavigationDrawer:
id: nav_drawer
radius: 0, dp(16), dp(16), 0
MDNavigationDrawerMenu:
MDNavigationDrawerHeader:
orientation:"vertical"
padding: (0, 0, 0, "12dp")
adaptive_height: True
MDLabel:
text: "Menu"
adaptive_height: True
#padding_x: "16dp"
font_style: "Display"
role: "small"
MDNavigationDrawerDivider:
MDNavigationDrawerItem:
active_indicator_color: "#e7e4c0"
#on_release: root.manager.current = "gender"
MDNavigationDrawerItemLeadingIcon:
icon: "book-open-variant"
theme_icon_color: "Custom"
icon_color: "#4a4939"
MDNavigationDrawerItemText:
text: "Words' Gender"
MDNavigationDrawerItem:
active_indicator_color: "#e7e4c0"
MDNavigationDrawerItemLeadingIcon:
icon: "pencil-plus"
theme_icon_color: "Custom"
icon_color: "#4a4939"
MDNavigationDrawerItemText:
text: "Personalize Dictionary"
MDNavigationDrawerItem:
active_indicator_color: "#e7e4c0"
MDNavigationDrawerItemLeadingIcon:
icon: "cog"
theme_icon_color: "Custom"
icon_color: "#4a4939"
MDNavigationDrawerItemText:
text: "Settings"
<Gender>:
name: "gender"
AnchorLayout:
anchor_x: "center"
anchor_y: "center"
BoxLayout:
orientation: "vertical"
Widget:
MDButton:
style: "outlined"
pos_hint: {"center_x": .5, "center_y": .5}
on_press: root.manager.current = "home"
MDButtonText:
text: "Home"
Widget:
MDCard:
size_hint: .85, 2
pos_hint: {"center_x": .5, "center_y": .5}
MDRelativeLayout:
MDLabel:
halign: "center"
id: extractedWord
markup: True
text: "extractedWord"
font_style: "Display"
color: (0,0,0,1)
BoxLayout:
spacing: 20
Widget:
MDButton:
on_press: root.check("der")
MDButtonText:
text: "[b]der[/b]"
markup: True
text_color: (1,1,1,1)
MDButton:
on_press: root.check("die")
MDButtonText:
text: "[b]die[/b]"
markup: True
text_color: (1,1,1,1)
MDButton:
on_press: root.check("das")
MDButtonText:
text: "[b]das[/b]"
markup: True
text_color: (1,1,1,1)
Widget:
Widget:
Widget:
Widget:
Widget:
巴纳德是正确的,需要完整的错误追溯,但你是说它是否等于以一种方式改变颜色,否则以另一种方式设置颜色。是否有可能非类型值进入 else 子句。