![](/img/trans.png)
[英]'super' object has no attribute '__getattr__' in kivymd
[英]kivymd: AttributeError: 'super' object has no attribute '__getattr__'
This.kv 文件我通過Builder.load_file('style.kv')
了 Kivy 文件
ScreenManager:
Sign_in:
Sign_up:
<Sign_in>:
name: 'sign in'
MDScreen:
id: sign_in
md_bg_color: [35/255, 59/255, 54/255, 1]
MDCard:
size_hint: None, None
size: 320, 400
pos_hint: {"center_x":.5, "center_y":.5}
elevation: 15
md_bg_color: [35/255, 49/255, 48/255, 1]
padding: 20
spacing: 30
orientation: "vertical"
MDLabel:
text: "LOGIN"
font_style: 'Button'
font_size: 45
halign: "center"
size_hint_y: None
height: self.texture_size[1]
padding_y: 15
MDTextFieldRound:
id: username
hint_text: "username"
icon_right: "account"
size_hint_x: None
width: 220
pos_hint: {"center_x":.5}
color_active: [1,1,1,1]
MDTextFieldRound:
id: Pass
hint_text: "Password"
icon_right: "eye-off"
size_hint_x: None
width: 220
pos_hint: {"center_x":.5}
password: True
color_active: [1,1,1,1]
MDTextFieldRound:
id: Email
hint_text: "Email Id"
icon_right: "email"
size_hint_x: None
width: 220
pos_hint: {"center_x":.5}
password: True
color_active: [1,1,1,1]
MDRoundFlatButton:
text: "Sign In"
pos_hint: {"center_x":.5}
font_size: 15
on_press: app.show_data()
Widget:
size_hint_y: None
height: 30
<Sign_up>:
name: 'sign up'
我嘗試了很多東西,但沒有解決這個錯誤.py 文件如何解決?
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
class Sign_in(Screen):
pass
class Sign_up(Screen):
pass
se = ScreenManager()
se.add_widget(Sign_in(name='sign in'))
se.add_widget(Sign_up(name='sign up'))
class Chatting(MDApp):
def build(self):
return Builder.load_file('style.kv')
def show_data(self):
print(self.root.ids.username.text)
if __name__ == '__main__':
Chatting().run()
代碼行:
print(self.root.ids.username.text)
正在嘗試通過您的App
的根小部件訪問username
id
,但根小部件是ScreenManager
,它不包含該id
。 根據您的kv
, username
id
在Sign_in
小部件中定義。
您可以使用ScreenManager
的get_screen()
方法訪問username
Screen
小部件,如下所示:
print(self.root.get_screen('sign in').ids.username.text)
請注意以下行:
se = ScreenManager()
se.add_widget(Sign_in(name='sign in'))
se.add_widget(Sign_up(name='sign up'))
正在創建另一個未在您的App
中實際使用的小部件樹。 使用的小部件樹由以下行構建:
return Builder.load_file('style.kv')
因此可以消除上面的那三行。
我想為任何未來的研究人員添加一件事是添加自我。 到您傳遞的任何變量。
例如,如果您想將小部件中的變量傳遞給應用程序,則必須在應用程序中將變量聲明為 self.some_variable = self.self.root.get_screen('sign in').ids.username.text。 然后,您可以使用此變量來執行功能。
在上面的代碼中,您可能希望將 username.text 與您存儲的用戶名進行比較。 如果你在你的應用程序中創建變量 some_variable = self.root.get_screen('sign in').ids.username.text,它會給你同樣的錯誤。
這花了我幾天的時間才弄清楚,所以我希望有一天它對某人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.