我试图将主窗口用作登录界面和用户界面的屏幕管理器。运行代码会出现上述错误。但是,没有主窗口的情况下,单个应用程序可以正常工作。
main.py:
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemeManager
from user.user import UserWindow
from signin.signin import LoginScreen
class MainWindow(BoxLayout):
user_widget = UserWindow()
signin_widget = LoginScreen()
def __init__(self, **kwargs):
self.id.scrn_si.add_widget(self.signin_widget)
self.id.scrn_usr.add_widget(self.signin_widget)
super().__init__(**kwargs)
class MainApp(MDApp):
def __init__(self, **kwargs):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
self.theme_cls.accent_palette = "Gray"
super().__init__(**kwargs)
def build(self):
return MainWindow()
if __name__=='__main__':
MainApp().run()
下面带有其相应的kv文件
main.kv:
<MainWindow>
id: main_win
ScreenManager:
id: scrn_mngr_main
Screen:
id: scrn_si
name: 'scrn_si'
Screen:
id: scrn_usr
name: 'scrn_usr'
接下来,我将看到signin.py
,这是登录屏幕。
signin / signin.py:
from kivymd.app import MDApp
from kivy.factory import Factory
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemeManager
from kivy.lang import Builder
Builder.load_file('signin/sign.kv')
class LoginScreen(BoxLayout):
def validate_user(self):
user = self.ids.email_field
pwd = self.ids.pwd_field
info = self.ids.info
uname = user.text
passw = pwd.text
if uname == '' or passw =='':
info.text = '[color=#FF0000]username and/ or password required[/color]'
else:
if uname == 'admin' and passw == 'admin':
info.text = '[color=#00FF00]Logged In Succesfully!![/color]'
else:
info.text = '[color=#FF0000]Invalid Username and/ or Password[/color]'
class SignApp(MDApp):
title = "LOGIN SCREEN"
def build(self):
return LoginScreen()
def show_password(self, field, button):
"""
Called when you press the right button in the password field
for the screen TextFields.
instance_field: kivy.uix.textinput.TextInput;
instance_button: kivymd.button.MDIconButton;
"""
# Show or hide text of password, set focus field
# and set icon of right button.
field.password = not field.password
field.focus = True
button.icon = 'eye' if button.icon == 'eye-off' else 'eye-off'
if __name__ == '__main__':
sa = SignApp()
sa.run()
其各自的.kv文件sign.kv
signin / sign.kv:
<LoginScreen>:
id: "login"
orientation:"vertical"
canvas:
Color:
rgb: 0.188235294,0.188235294,0.188235294
Rectangle:
#source: "mountain2.png"
pos: self.pos
size: self.size
BoxLayout:
orientation: "vertical"
size_hint: .8, .5
pos_hint: {"center_x": .5, "center_y": .5}
height: self.minimum_height
spacing: dp(15)
pos_hint: {"center_x": .5, "center_y": .5}
Label:
id: info
text: ''
markup: True
MDTextFieldRound:
id: email_field
icon_left: "email"
icon_right: "account-box"
icon_right_disabled: True
size_hint: 1, None
pos_hint: {"center_x": .5, "center_y": .5}
hint_text: "Email"
foreground_color: 0, 0, 0, 0.8
focus: True
on_text_validate: pwd_field.focus = True
MDTextFieldRound:
id: pwd_field
icon_type: "all"
icon_left: "key-variant"
icon_right: "eye-off"
icon_right_disabled: False
#icon_callback: app.show_password
password: True
size_hint: 1, None
pos_hint: {"center_x": .5, "center_y": .5}
hint_text: "Password"
foreground_color: 0, 0, 0, 0.8
on_text_validate: root.validate_user()
MDFillRoundFlatButton:
text: "Login"
size_hint: .9, None
pos_hint: {"center_x": .5, "center_y": .5}
md_bg_color: 1, 0.64705058823, 0, 1
on_release: root.validate_user()
MDTextButton:
text: "Forgot your password?"
pos_hint: {"center_x": .75}
color: 1, 1, 1, 1
最后是用户屏幕user.py
user / user.py:
from kivymd.app import MDApp
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.tab import MDTabsBase
from kivymd.theming import ThemeManager
from kivymd.uix.menu import MDDropdownMenu
from kivy.factory import Factory
from kivy.lang import Builder
Builder.load_file('user/user.kv')
class MyTab(BoxLayout, MDTabsBase):
pass
class MyTab2(BoxLayout, MDTabsBase):
pass
class MyTab3(BoxLayout, MDTabsBase):
pass
class UserWindow(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class UserApp(MDApp):
title = 'DEMO'
dropdown = ObjectProperty()
def __init__(self, **kwargs):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
self.theme_cls.accent_palette = "Gray"
super().__init__(**kwargs)
def on_start(self):
# Create the dropdown menu
self.dropdown = MDDropdownMenu(width_mult=2)
# Add items to the menu
for i in range(6):
self.dropdown.items.append(
{"viewclass": "MDMenuItem",
"text": "Option " + str(i),
"callback": self.option_callback}
)
def option_callback(self, text_of_the_option):
print(text_of_the_option)
def build(self):
screen = Factory.UserWindow()
tab = MyTab(text='Home Tab')
screen.ids.android_tabs.add_widget(tab)
tab = MyTab2(text='Approved Tickets')
screen.ids.android_tabs.add_widget(tab)
tab = MyTab3(text='History')
screen.ids.android_tabs.add_widget(tab)
return screen
if __name__ == '__main__':
oa = UserApp()
oa.run()
下一个是user.kv
文件
user / user.kv:
<UserWindow>
orientation: 'vertical'
MDToolbar:
title: app.title
md_bg_color: app.theme_cls.primary_color
background_palette: 'Primary'
left_action_items: [['menu', lambda x: app.dropdown.open(x)]]
MDTabs:
id: android_tabs
<MyTab>
FloatLayout:
MDRaisedButton:
text: "Open day picker"
pos_hint: {'center_x': .5, 'center_y': .4}
on_release:
app.dropdown.open(self)
MDRaisedButton:
text: "Open destination picker"
pos_hint: {'center_x': .5, 'center_y': .8}
<MyTab2>
FloatLayout:
<MyTab3>
FloatLayout:
这是我得到的错误
Traceback (most recent call last):
File "/home/pakomoaro/Desktop/INTERFACE/DEMO/main.py", line 8, in <module>
class MainWindow(BoxLayout):
File "/home/pakomoaro/Desktop/INTERFACE/DEMO/main.py", line 9, in MainWindow
user_widget = UserWindow()
File "/home/pakomoaro/Desktop/INTERFACE/DEMO/user/user.py", line 26, in __init__
super().__init__(**kwargs)
File "/home/pakomoaro/.local/lib/python3.6/site-packages/kivy/uix/boxlayout.py", line 145, in __init__
super(BoxLayout, self).__init__(**kwargs)
File "/home/pakomoaro/.local/lib/python3.6/site-packages/kivy/uix/layout.py", line 76, in __init__
super(Layout, self).__init__(**kwargs)
File "/home/pakomoaro/.local/lib/python3.6/site-packages/kivy/uix/widget.py", line 361, in __init__
rule_children=rule_children)
File "/home/pakomoaro/.local/lib/python3.6/site-packages/kivy/uix/widget.py", line 469, in apply_class_lang_rules
rule_children=rule_children)
File "/home/pakomoaro/.local/lib/python3.6/site-packages/kivy/lang/builder.py", line 538, in apply
rule_children=rule_children)
File "/home/pakomoaro/.local/lib/python3.6/site-packages/kivy/lang/builder.py", line 654, in _apply_rule
child = cls(__no_builder=True)
File "/home/pakomoaro/.local/lib/python3.6/site-packages/kivymd/uix/toolbar.py", line 191, in __init__
super().__init__(**kwargs)
File "/home/pakomoaro/.local/lib/python3.6/site-packages/kivymd/theming.py", line 369, in __init__
App.get_running_app().property("theme_cls", True),
AttributeError: 'NoneType' object has no attribute 'property'