KivyMD将main用于事件,将kv文件用于布局



我不想这样做:

class SmashLadderApp(MDApp):
def build(self):
return Builder.load_file("View.kv")

但是像这样的东西:

class WindowManager(ScreenManager): 
pass
#kv =Builder.load_file("View.kv")
#add screen widgits and load file
wm = WindowManager()
screens = [LoginPage(name="LoginPageID"),LadderPage(name="LadderPageID"), RegisterPage(name ="RegisterPageID")]
for screen in screens:
wm.add_widget(screen)
#starting page
wm.current = "LoginPageID"
class SmashLadderApp(MDApp):
def build(self):
kv =Builder.load_file("View.kv")       
return wm
SmashLadderApp().run()

底部返回一个空白窗口。如何指定生成器,然后在kv文件中具有widgits布局?我主要想处理这些事件。第二个片段中注释掉的kv =Builder.load_file("View.kv")是我认为您可以实现这一点的方式,但给了我一个错误。

我确信这在Kivy中有效,但在KivyMD中无效。代码也粘贴在后面。为了可读性,代码发布在这里:

Python(文件名=Display.py(:https://paste.nextcord.dev/?id=1659041664901348

KV文件(file_name=View.KV(:https://paste.nextcord.dev/?id=1659041570568365

Display.py:

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
#All functions/method handles
class LoginPage(Screen):

def register(self):
wm.current="RegisterPageID"
class LadderPage(Screen):

def user_page(self):
pass
def mod_page(self):
pass
def dev_page(self):
pass
class RegisterPage(Screen):
pass
class WindowManager(ScreenManager): 
pass
#kv =Builder.load_file("View.kv")                     
#add screen widgits and load file
wm = WindowManager()
screens = [LoginPage(name="LoginPageID"),LadderPage(name="LadderPageID"), RegisterPage(name ="RegisterPageID")]
for screen in screens:
wm.add_widget(screen)
#starting page
wm.current = "LoginPageID"
class SmashLadderApp(MDApp):
def build(self):
kv =Builder.load_file("View.kv")              
return wm
SmashLadderApp().run()

视图.kv:

#:kivy 2.1
# WindowManager:
#     LoginPage:
#     RegisterPage:
#     LadderPage:
#Default widgits, almost like methods
<MDLabel>:
size_hint: .2,.1
color: 0,0,0,1
bold: True
<MDTextField>:
hint_text_color_normal:1,1,1,1
# color_mode: "custom"
hint_text_color_focus: 1,1,1,1
helper_text_color_focus: 1,1,1,1
line_color_normal: 1,1,1,1
line_color_focus: 1,1,1,1
# icon_right: "KirbyHeart.ico"
<MDRectangleFlatButton>:
theme_text_color: "Custom"
text_color: 1,1,1,1
line_color: 1,1,1,0
#pages
<LoginPage>:
name: "LoginPageID"
#create opacity
canvas:
Color:
rgba: 0.1,0.1,0.1,.8
#creates background
Rectangle:
source: "ssbu.jpg"
pos: self.pos
size: self.size
FloatLayout:
#Places kirby heart in certain location, don't know how to make Kirby stay next to text input
Image: 
source:"KirbyHeart.ico"
pos_hint: {"center_x": .65, "center_y": .50}
#just a label
MDLabel:
#can use mark up language
markup: True
text: "[font=times]Smash    Laddern        Login[/font]"                              
font_size: 20
#relative size to window
pos_hint:{"center_x":.52,"center_y": .58}
color: "white"
MDTextField:
id: usernameID
hint_text: "please enter your username"   
helper_text: "Click register if you haven't yet"
helper_text_mode:"on_focus"
pos_hint:{"center_x":.5, "center_y":.5}
size_hint_x: None
width: 200
MDTextField:
id: passwordID
hint_text: "Enter your password. "
pos_hint:{"center_x":.5, "center_y":.4}
size_hint_x: None
width: 200
MDRectangleFlatButton:
text: "Register?"
text_color: 0,0,1,1
pos_hint:{"center_x":.595, "center_y":.35}
size_hint_x: None
on_press:
root.manager.transition.direction: "up"
root.register()
on_release:

<LadderPage>:
name: "LadderPageID"
<RegisterPage>:
#Testing Layout
name: "RegisterPageID"
FloatLayout:
Image: 
source:"KirbyHeart.jpg"
pos_hint: {"center_x": .5, "center_y": .5}
MDRectangleFlatButton:
id: jokeID
text: "Register?"
text_color: 0,0,1,1
pos_hint:{"center_x":.59

如果我从构建方法中删除Builder对象并将其插入WindowManager类下面;我会得到这个错误:

Traceback (most recent call last):
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivymdtheming.py", line 1216, in __init__
App.get_running_app().property("theme_cls", True),
AttributeError: 'NoneType' object has no attribute 'property'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:UsersChapmGitHubLadder-matchmakingUIDisplay.py", line 30, in <module>
screens = [LoginPage(name="LoginPageID"),LadderPage(name="LadderPageID"), RegisterPage(name ="RegisterPageID")]
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivyuixrelativelayout.py", line 274, in __init__
super(RelativeLayout, self).__init__(**kw)
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivyuixfloatlayout.py", line 65, in __init__
super(FloatLayout, self).__init__(**kwargs)
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivyuixlayout.py", line 76, in __init__
super(Layout, self).__init__(**kwargs)
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivyuixwidget.py", line 366, in __init__
self.apply_class_lang_rules(
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivyuixwidget.py", line 470, in apply_class_lang_rules
Builder.apply(
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivylangbuilder.py", line 540, in apply
self._apply_rule(
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivylangbuilder.py", line 662, in _apply_rule
self._apply_rule(
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivylangbuilder.py", line 658, in _apply_rule
child = cls(__no_builder=True)
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivymduixlabellabel.py", line 327, in __init__
super().__init__(**kwargs)
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivymduixbehaviorsdeclarative_bahavior.py", line 302, in __init__
super().__init__(**kwargs)
File "C:UsersChapmAppDataLocalProgramsPythonPython310libsite-packageskivymdtheming.py", line 1224, in __init__
raise ValueError(
ValueError: KivyMD: App object must be initialized before loading root widget. See https://github.com/kivymd/KivyMD/wiki/Modules-Material-App#exceptions

只需将所有内容移动到build作为

def build(self):
Builder.load_file("View.kv") # You can place it outside too.
wm = WindowManager()
screens = [LoginPage(name="LoginPageID"),LadderPage(name="LadderPageID"), RegisterPage(name ="RegisterPageID")]
for screen in screens:
wm.add_widget(screen)
return wm

请记住,wm不再是全局的,所以像这样的wm.some_prop_or_method将引发NameError

最新更新