簡體   English   中英

如何將用戶在kivyMD文本框中輸入的文本轉為python文件中的變量?

[英]How do I turn the text that is inputed by a user into a kivyMD text box to a variable in python file?

我對 Kivy/KivyMD 比較陌生。 我不知道如何在按下注冊按鈕時從用戶名和密碼文本字段中獲取值並將它們轉換為我的 python 文件中的變量。

當前,單擊“注冊”按鈕時,它只會打印“已單擊”。

任何幫助將不勝感激!

下面是我的 python 文件:

from kivymd.app import MDApp
from kivymd.uix.relativelayout import MDRelativeLayout

from kivy.lang import Builder
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen, SwapTransition

Builder.load_file('ALevelNEA.kv')

##Below are the classes for my different screens

class SignupScreen(Screen):
    pass

class MenuScreen(Screen):
    pass

class SigninScreen(Screen):
    pass

## Below are the classes for my custom buttons, widgets and text fields

class PasswordField(MDRelativeLayout):
    text = StringProperty()
    hint_text = StringProperty()
    pass

class EmailField(MDRelativeLayout):
    text = StringProperty()
    hint_text = StringProperty()
    #email_field = ObjectProperty(None)

##Below is the code for my main app

class MainApp(MDApp):
    def build(self):
        sm = ScreenManager(transition= SwapTransition())
        sm.add_widget(MenuScreen(name='menu'))
        sm.add_widget(SigninScreen(name = 'signin'))
        sm.add_widget(SignupScreen(name='signup'))
        return sm

    def RecordCheck(self):
        print("clicked")

    def AddToRecord(self):
        print("clicked")


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

下面是我的 KV 文件:

#Below are my different screens

#Below is the Menu Screen

<MenuScreen>:
    Screen:
        Image:
            source: "Logonb.png"
            size_hint_x: 0.4
            size_hint_y: 0.6
            allow_stretch: True
            pos_hint: {"center_x": 0.5, "center_y": 0.6 }

        MDFillRoundFlatButton:
            id: SignIn
            text: "Sign in with email"
            pos_hint: {"center_x": 0.5, "center_y": 0.2}
            md_bg_color: 0.3607, 0.3882, 1
            size_hint: 0.225, 0
            on_release:
                root.manager.current = "signin"

        MDRoundFlatButton:
            text: "Sign up"
            pos_hint: {"center_x": 0.5, "center_y": 0.12}
            text_color: 0.3607, 0.3882, 1
            line_color: 0, 0, 0
            on_release:
                root.manager.current = "signup"

        MDLabel:
            text: "by continuing you agree to the"
            font_size: 15
            halign: 'center'
            pos_hint: {"center_y": 0.07}

        MDTextButton:
            text: "Terms and Conditions"
            font_size: 15
            pos_hint: {"center_x": 0.5, "center_y": 0.0525}
            theme_text_color: "Custom"
            text_color: 0.3607, 0.3882, 1

# Below is my sign in Screen

<SigninScreen>:
    Screen:
        MDIconButton:
            icon: "keyboard-backspace"
            pos_hint: {"center_x": 0.35, "center_y": 0.95}
            on_release:
                root.manager.current = "menu"

        Image:
            source: "LoginImage.png"
            size_hint_x: 0.4
            size_hint_y: 0.6
            allow_stretch: True
            pos_hint: {"center_x": 0.5, "center_y": 0.7 }

        MDLabel:
            text: "Sign in"
            halign: "center"
            pos_hint: {"center_x": 0.387, "center_y": 0.45}
            font_size: 30
            font_style: "H5"

        EmailField:
            hint_text: "Email ID"
            pos_hint: {"center_x": 0.5, "center_y": 0.375}
            size_hint_x: None
            width: "250dp"

        PasswordField:
            size_hint_x: None
            width: "250dp"
            hint_text: "Password"
            pos_hint: {"center_x": 0.5, "center_y": 0.3}

        MDTextButton:
            text: "Forgot password?"
            font_size: 15
            pos_hint: {"center_x": 0.62, "center_y": 0.255}
            theme_text_color: "Custom"
            text_color: 0.3607, 0.3882, 1

        MDFillRoundFlatButton:
            text: "Sign in"
            pos_hint: {"center_x": 0.5, "center_y": 0.2}
            md_bg_color: 0.3607, 0.3882, 1
            size_hint: 0.225, 0
            on_release: app.RecordCheck()

        MDLabel:
            text: "by continuing you agree to the"
            font_size: 15
            halign: 'center'
            pos_hint: {"center_y": 0.15}

        MDTextButton:
            text: "Terms and Conditions"
            font_size: 15
            pos_hint: {"center_x": 0.5, "center_y": 0.1325}
            theme_text_color: "Custom"
            text_color: 0.3607, 0.3882, 1

#Below is the sign up screen
<SignupScreen>:

    Screen:
        EmailField:
            hint_text: "Email"
            pos_hint: {"center_x": 0.5, "center_y": 0.375}
            size_hint_x: None
            width: "250dp"
            on_text: root.email = self.text

        PasswordField:
            size_hint_x: None
            width: "250dp"
            hint_text: "Password"
            pos_hint: {"center_x": 0.5, "center_y": 0.3}
            on_text: root.password = self.text

        MDRoundFlatButton:
            text: "Sign up"
            pos_hint: {"center_x": 0.5, "center_y": 0.12}
            text_color: 0.3607, 0.3882, 1
            line_color: 0, 0, 0
            on_release: app.AddToRecord()

#Custom buttons and text fields are located below

<PasswordField>:
    size_hint_y: None
    height: text_field.height

    MDTextField:
        id: text_field
        hint_text: root.hint_text
        text: root.text
        password: True
        icon_left: "lock"

    MDIconButton:
        icon: "eye-off"
        pos_hint: {"center_y": .5}
        pos: text_field.width - self.width + dp(8), 0
        theme_text_color: "Hint"
        on_release:
            self.icon = "eye" if self.icon == "eye-off" else "eye-off"
            text_field.password = False if text_field.password is True else True

<EmailField>:
    size_hint_y: None
    height: email_field.height

    MDTextField:
        id: email_field
        hint_text: root.hint_text
        text: root.text
        icon_left: "email"

對於email,可以使用ScreenManagerget_screen()方法和一些ids來訪問包含文本的MDTextField 首先,向EmailField添加一個id

    EmailField:
        id: emailfield  # added id
        hint_text: "Email ID"
        pos_hint: {"center_x": 0.5, "center_y": 0.375}
        size_hint_x: None
        width: "250dp"

然后使用新的idget_screen()RecordCheck()方法中訪問MDTextField

def RecordCheck(self):
    print("clicked")
    signinScreen = self.root.get_screen('signin')
    email = signinScreen.ids.emailfield.ids.email_field.text
    print('email:', email)

可以使用非常相似的方法來訪問密碼。

id屬性添加到 email 和密碼小部件:

    EmailField:
        id: email_widget

    PasswordField:
        id: pass_widget

您可以將任何其他參數傳遞給您的方法,例如替換:

on_release: app.AddToRecord()

on_release: app.AddToRecord(root.ids.email_widget.ids.email_field.text, root.ids.pass_widget.ids.text_field.text)

然后將您的AddToRecord方法定義更改為:

def AddToRecord(self, email, passw):
    print("clicked")
    print(f'email: {email}, password: {passw}')

暫無
暫無

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

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