簡體   English   中英

KivyMD - ScreenManager 和 ids

[英]KivyMD - ScreenManager and ids

大家都好嗎? 我正在學習 KivyMD 和 ScreenManager,我遇到了一個小問題(也是一個大問題)。 我正在編寫一個允許用戶登錄或注冊的程序(就像大多數初學者所做的那樣)(我也在練習 sqlite3,但我對此沒有問題)。 首先,我嘗試在一個屏幕上完成所有操作,而且效果很好!

但是現在,當我嘗試使用任何 function 時,我得到:

_username = self.root.ids.my_username.text

AttributeError: 'super' object has no attribute '__getattr__'

py文件是:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import sqlite3

class MainMenu(Screen):
    pass

class LogIn(Screen):
    pass

class SignUp(Screen):
    pass

class MainLogSignApp(MDApp):

    def build(self):

        # Connect to databse an create a cursor
        conn = sqlite3.connect('user_pass.db')
        c = conn.cursor()

        c.execute("""CREATE TABLE if not exists user_name(
                username text,
                password text
            )""")

        conn.commit()
        conn.close()

        return Builder.load_file('mainlogsign.kv')


        return sm

    def submit_info(self):
        '''If the info does not exist, it adds it to the table;
        If it does, the function check if it is correct'''

        _username = self.root.ids.my_username.text
        _password = self.root.ids.my_password.text

        # Connect and create cursor
        conn = sqlite3.connect('user_pass.db')
        c = conn.cursor()

        # This should not be necesary, but it is here just in case I delete the table using     the DELETE button.
        c.execute("""CREATE TABLE if not exists user_name(
                username text,
                password text
            )""")

        c.execute("SELECT * FROM user_name WHERE username = (?)", (_username,))
        already_in = c.fetchone()

        if already_in and already_in[0]:
            self.root.ids.my_message.text = already_in[0] + '\nUser already exists'
        else:
            c.execute("INSERT INTO user_name VALUES (?,?)", (_username, _password))
            self.root.ids.my_message.text = 'User added successfully'

        conn.commit()
        conn.close()

        self.root.ids.my_username.text = ''
        self.root.ids.my_password.text = ''


# THERE ARE MORE FUNCTIONS, BUT THEY ALL HAVE THE SAME PROBLEM.

if __name__ == '__main__':
    MainLogSignApp().run()

kivy 文件是:

ScreenManager:
    MainMenu:
    LogIn:
    SignUp:

<MyTextField@MDTextFieldRound>:
    font_size: 12
    size_hint_x: 0.6
    pos_hint: {'center_x': 0.5}
    halign: 'center'


<MainMenu>:
    name: 'main_menu'

    BoxLayout:
        orientation: 'vertical'

        Label:
            size_hint: .1, .3

        MDRoundFlatButton:
            text: 'LOG IN'
            pos_hint: {'center_x': .5}
            on_press: 
                root.manager.current = 'login'
                root.manager.transition.direction = 'left'

        Label:
            size_hint: .1, .1

        MDRoundFlatButton:
            text: 'SIGN UP'
            pos_hint: {'center_x': .5}
            on_press: 
                root.manager.current = 'signup'
                root.manager.transition.direction = 'left'

        Label:
            size_hint: .1, .3


<LogIn>:
    name: 'login'

    BoxLayout:
        orientation: 'vertical'
    
        MDLabel:
            text: 'LOGIN'
            halign: 'center'
        
        Button:
            id: my_label_l
            text: 'DELETE'
            size_hint: 0.6, 0.2
            pos_hint: {'center_x': .5}
            halign: 'center'
            on_press: app.delete_record()

        MyTextField:
            id: my_username_l
            hint_text: 'Enter your username'
            icon_right: 'account'

        MyTextField:
            id: my_password_l
            hint_text: 'Enter your password'
            icon_right: 'eye-outline'

        Button:
            id: log_in
            text: 'LOG IN'
            size_hint: 0.6, 0.2
            pos_hint: {'center_x': .5}
            on_press: app.log_in()

        Button:
            id: show_button_l
            text: 'Nothing'
            size_hint: 0.6, 0.2
            pos_hint: {'center_x': .5}

        MDLabel:
            id: my_message_l
            text: ''
            halign: 'center'

        MDRectangleFlatIconButton:
            icon: 'backspace-outline'
            on_press: 
                root.manager.current = 'main_menu'
                root.manager.transition.direction = 'right'



<SignUp>:
    name: 'signup'

    BoxLayout:
        orientation: 'vertical'
        MDLabel:
            text: 'SIGNUP'
            halign: 'center'

        Button:
            id: my_label
            text: 'DELETE'
            size_hint: 0.6, 0.2
            pos_hint: {'center_x': .5}
            halign: 'center'
            on_press: app.delete_record()

        MyTextField:
            id: my_username
            hint_text: 'Enter your username'
            icon_right: 'account'

        MyTextField:
            id: my_password
            hint_text: 'Enter your password'
            icon_right: 'eye-outline'

        Button:
            id: submit_button
            text: 'Submit info'
            size_hint: 0.6, 0.2
            pos_hint: {'center_x': .5}
            on_press: app.submit_info()

        Button:
            id: show_button
            text: 'Show records'
            size_hint: 0.6, 0.2
            pos_hint: {'center_x': .5}
            on_press: app.show_info()

        MDLabel:
            id: my_message
            text: ''
            halign: 'center'

        MDRectangleFlatIconButton:
            icon: 'backspace-outline'
            on_press: 
                root.manager.current = 'main_menu'
                root.manager.transition.direction = 'right'

這些函數都不起作用,所以我猜它與“app.function()”或 id 有關,但它確實在第一次起作用。 也許我必須使用不同的路徑,但我不知道。

作為一個小問題,當我在 TextInput 上書寫時,提示文本不會消失。 知道為什么嗎?

謝謝!

順便說一句,您可能會注意到“刪除”和“顯示記錄”按鈕,並認為它們與登錄或注冊程序無關。 你說的再對不過了,所以別理他們。 我只添加了這些按鈕以使用 sqlite3,但在真正的應用程序或網站上它們不會存在。 盡管如此,任何按鈕的問題都是一樣的。

好的,我已經解決了這個問題。 我將 function submit_info 寫入 SignUp class 並將 app.submit_info() 更改為 root.submit_info() (在 kv 文件中)。 另外,我必須刪除每個“根”。 在 function。

如果有人遇到同樣的問題,我希望這能奏效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM