首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >KivyMD从textfields获取数据

KivyMD从textfields获取数据
EN

Stack Overflow用户
提问于 2018-07-04 16:30:12
回答 2查看 8K关注 0票数 0

自从几个星期以来,我开始使用KivyMD,我爱上了这个Kivy主题。我的问题是:我正在使用KivyMD制作一个多屏幕应用程序,我的第一个屏幕是“登录屏幕”。我想在我的主类中添加一个函数,它得到我的“用户名”文本字段和我的“密码”文本字段的值。如果标识符是正确的,我需要‘屏幕2’。

这是我的代码:

代码语言:javascript
复制
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemeManager

class MyLayout(BoxLayout):

    scr_mngr = ObjectProperty(None)

    def change_screen(self, screen, *args):
        self.scr_mngr.current = screen


KV = """
#:import Toolbar kivymd.toolbar.Toolbar
#:import ThemeManager kivymd.theming.ThemeManager
#:import MDNavigationDrawer kivymd.navigationdrawer.MDNavigationDrawer
#:import NavigationLayout kivymd.navigationdrawer.NavigationLayout
#:import NavigationDrawerDivider 
kivymd.navigationdrawer.NavigationDrawerDivider
#:import NavigationDrawerToolbar 
kivymd.navigationdrawer.NavigationDrawerToolbar
#:import NavigationDrawerSubheader 
kivymd.navigationdrawer.NavigationDrawerSubheader
#:import MDCheckbox kivymd.selectioncontrols.MDCheckbox
#:import MDSwitch kivymd.selectioncontrols.MDSwitch
#:import MDList kivymd.list.MDList
#:import OneLineListItem kivymd.list.OneLineListItem
#:import TwoLineListItem kivymd.list.TwoLineListItem
#:import ThreeLineListItem kivymd.list.ThreeLineListItem
#:import OneLineAvatarListItem kivymd.list.OneLineAvatarListItem
#:import OneLineIconListItem kivymd.list.OneLineIconListItem
#:import OneLineAvatarIconListItem kivymd.list.OneLineAvatarIconListItem
#:import MDTextField kivymd.textfields.MDTextField
#:import MDSpinner kivymd.spinner.MDSpinner
#:import MDCard kivymd.card.MDCard
#:import MDSeparator kivymd.card.MDSeparator
#:import MDDropdownMenu kivymd.menu.MDDropdownMenu
#:import get_color_from_hex kivy.utils.get_color_from_hex
#:import colors kivymd.color_definitions.colors
#:import SmartTile kivymd.grid.SmartTile
#:import MDSlider kivymd.slider.MDSlider
#:import MDTabbedPanel kivymd.tabs.MDTabbedPanel
#:import MDTab kivymd.tabs.MDTab
#:import MDProgressBar kivymd.progressbar.MDProgressBar
#:import MDAccordion kivymd.accordion.MDAccordion
#:import MDAccordionItem kivymd.accordion.MDAccordionItem
#:import MDAccordionSubItem kivymd.accordion.MDAccordionSubItem
#:import MDThemePicker kivymd.theme_picker.MDThemePicker
#:import MDBottomNavigation kivymd.tabs.MDBottomNavigation
#:import MDBottomNavigationItem kivymd.tabs.MDBottomNavigationItem

#:import partial functools.partial

MyLayout:
    scr_mngr: scr_mngr
    orientation: 'vertical'

    ScreenManager:
        id: scr_mngr
        Screen:
            name: 'screen1'
            MDCard:
                size_hint: None, None
                size: dp(520), dp(340)
                pos_hint: {'center_x': 0.5, 'center_y': 0.5}
                BoxLayout:
                    orientation:'vertical'
                    padding: dp(20)
                    spacing:20
                    MDLabel:
                        text: 'Connexion'
                        theme_text_color: 'Secondary'
                        font_style:"Title"
                        size_hint_y: None
                        height: dp(36)
                    MDSeparator:
                        height: dp(1)
                    MDTextField:
                        id: 'username'
                        hint_text: "Username "
                        helper_text_mode: "on_focus"
                    MDTextField:
                        id: 'password'
                        hint_text: "Password "
                        helper_text_mode: "on_focus"

                    MDFlatButton:
                        text: "Connexion"
                        pos_hint: {'center_x': 0.5}
                        on_release: root.check_data_login()
        Screen:
            name: 'screen2'
            Toolbar:
                id: toolbar
                title: "Welcome ! "
                pos_hint: {'center_x': 0.5, 'center_y': 0.97}
                md_bg_color: app.theme_cls.primary_color
                background_palette: 'DeepPurple'
                background_hue: 'A400'
                left_action_items: [['arrow-left', p 
                    partial(root.change_screen, 'screen1') ]]
                right_action_items: [['animation', lambda x: M 
                    MDThemePicker().open()]]

            MDLabel:
                font_style: 'Title'
                theme_text_color: 'Primary'
                text: "Data :"
                height: self.texture_size[1] + dp(3)
                halign: 'center'
                pos_hint: {'center_x': 0.5, 'center_y': 0.85}




"""


class MyApp(App):
    theme_cls = ThemeManager()

    def build(self):
        return Builder.load_string(KV)


    def check_data_login(self):
        username = self.username.text
        password= self.password.text

        print(username)
        print(password)

MyApp().run()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-04 21:04:21

解决方案

解决方案是使用Kivy ObjectProperty连接到ids。详情请参阅示例。

kv文件

添加以下id和ObjectProperties

  1. 添加id: screen1
  2. 添加ObjectProperty,screen1: screen1
  3. id不是字符串。从id: usernameid: password中删除引号
  4. 添加ObjectProperty,username: username
  5. 添加ObjectProperty,password: password
  6. password: True添加到TextInput,以便隐藏密码

片段- kv文件

代码语言:javascript
复制
ScreenManager:
    id: scr_mngr
    screen1: screen1

    Screen:
        id: screen1
        name: 'screen1'
        username: username
        password: password

        MDCard:
            ...
                MDTextField:
                    id: username
                    hint_text: "Username "
                    helper_text_mode: "on_focus"

                MDTextField:
                    id: password
                    hint_text: "Password "
                    helper_text_mode: "on_focus"
                    password: True

Python文件

  1. check_data_login()方法从class MyApp()移动到class MyLayout()
  2. self.username.text替换为self.scr_mngr.screen1.username.text
  3. self.password.text替换为self.scr_mngr.screen1.password.text
  4. 添加了if语句以检查用户名和密码

代码片段- Python文件

代码语言:javascript
复制
class MyLayout(BoxLayout):

    scr_mngr = ObjectProperty(None)

    def check_data_login(self):
        username = self.scr_mngr.screen1.username.text
        password = self.scr_mngr.screen1.password.text

        print(username)
        print(password)

        if username == "KivyMD" and password == "kivy":
            self.change_screen("screen2")

Kivy文档

kv文件的语法

如果小部件没有具有给定名称的属性,则将自动创建ObjectProperty并将其添加到小部件中。

引用小部件ids

警告 当将值赋值给id时,请记住该值不是字符串。没有引号:好的-> id: value,坏的-> id:'value‘

在python代码中访问Kv中定义的小部件

使用ObjectProperty通常被认为是“最佳实践”。这将创建一个直接引用,提供更快的访问速度,并且更加显式。

示例

main.py

代码语言:javascript
复制
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemeManager


class MyLayout(BoxLayout):

    scr_mngr = ObjectProperty(None)

    def check_data_login(self):
        username = self.scr_mngr.screen1.username.text
        password = self.scr_mngr.screen1.password.text

        print(username)
        print(password)

        if username == "KivyMD" and password == "kivy":
            self.change_screen("screen2")

    def change_screen(self, screen, *args):
        self.scr_mngr.current = screen


KV = """
#:import Toolbar kivymd.toolbar.Toolbar
#:import ThemeManager kivymd.theming.ThemeManager
#:import MDNavigationDrawer kivymd.navigationdrawer.MDNavigationDrawer
#:import NavigationLayout kivymd.navigationdrawer.NavigationLayout
#:import NavigationDrawerDivider kivymd.navigationdrawer.NavigationDrawerDivider
#:import NavigationDrawerToolbar kivymd.navigationdrawer.NavigationDrawerToolbar
#:import NavigationDrawerSubheader kivymd.navigationdrawer.NavigationDrawerSubheader
#:import MDCheckbox kivymd.selectioncontrols.MDCheckbox
#:import MDSwitch kivymd.selectioncontrols.MDSwitch
#:import MDList kivymd.list.MDList
#:import OneLineListItem kivymd.list.OneLineListItem
#:import TwoLineListItem kivymd.list.TwoLineListItem
#:import ThreeLineListItem kivymd.list.ThreeLineListItem
#:import OneLineAvatarListItem kivymd.list.OneLineAvatarListItem
#:import OneLineIconListItem kivymd.list.OneLineIconListItem
#:import OneLineAvatarIconListItem kivymd.list.OneLineAvatarIconListItem
#:import MDTextField kivymd.textfields.MDTextField
#:import MDSpinner kivymd.spinner.MDSpinner
#:import MDCard kivymd.card.MDCard
#:import MDSeparator kivymd.card.MDSeparator
#:import MDDropdownMenu kivymd.menu.MDDropdownMenu
#:import get_color_from_hex kivy.utils.get_color_from_hex
#:import colors kivymd.color_definitions.colors
#:import SmartTile kivymd.grid.SmartTile
#:import MDSlider kivymd.slider.MDSlider
#:import MDTabbedPanel kivymd.tabs.MDTabbedPanel
#:import MDTab kivymd.tabs.MDTab
#:import MDProgressBar kivymd.progressbar.MDProgressBar
#:import MDAccordion kivymd.accordion.MDAccordion
#:import MDAccordionItem kivymd.accordion.MDAccordionItem
#:import MDAccordionSubItem kivymd.accordion.MDAccordionSubItem
#:import MDThemePicker kivymd.theme_picker.MDThemePicker
#:import MDBottomNavigation kivymd.tabs.MDBottomNavigation
#:import MDBottomNavigationItem kivymd.tabs.MDBottomNavigationItem

#:import partial functools.partial

MyLayout:
    scr_mngr: scr_mngr
    orientation: 'vertical'

    ScreenManager:
        id: scr_mngr
        screen1: screen1

        Screen:
            id: screen1
            name: 'screen1'
            username: username
            password: password

            MDCard:
                size_hint: None, None
                size: dp(520), dp(340)
                pos_hint: {'center_x': 0.5, 'center_y': 0.5}

                BoxLayout:
                    orientation:'vertical'
                    padding: dp(20)
                    spacing:20

                    MDLabel:
                        text: 'Connexion'
                        theme_text_color: 'Secondary'
                        font_style:"Title"
                        size_hint_y: None
                        height: dp(36)

                    MDSeparator:
                        height: dp(1)

                    MDTextField:
                        id: username
                        hint_text: "Username "
                        helper_text_mode: "on_focus"

                    MDTextField:
                        id: password
                        hint_text: "Password "
                        helper_text_mode: "on_focus"
                        password: True

                    MDFlatButton:
                        text: "Connexion"
                        pos_hint: {'center_x': 0.5}
                        on_release: root.check_data_login()
        Screen:
            name: 'screen2'

            Toolbar:
                id: toolbar
                title: "Welcome ! "
                pos_hint: {'center_x': 0.5, 'center_y': 0.97}
                md_bg_color: app.theme_cls.primary_color
                background_palette: 'DeepPurple'
                background_hue: 'A400'
                left_action_items: [['arrow-left', partial(root.change_screen, 'screen1') ]]
                right_action_items: [['animation', lambda x: MDThemePicker().open()]]

            MDLabel:
                font_style: 'Title'
                theme_text_color: 'Primary'
                text: "Data :"
                height: self.texture_size[1] + dp(3)
                halign: 'center'
                pos_hint: {'center_x': 0.5, 'center_y': 0.85}
"""


class MyApp(App):
    title = "Kivy MD Demo"
    theme_cls = ThemeManager()

    def build(self):
        return Builder.load_string(KV)


MyApp().run()

输出

票数 7
EN

Stack Overflow用户

发布于 2020-09-30 14:43:16

另一种有按钮的方法。你的kv档案:

代码语言:javascript
复制
KV = """
Screen:
    
    
    MDLabel:
        id: direct
        text: 'Paste File Path: '
        color: [0.6,0.5,0.3,1,1]
                
        pos_hint: {"center_x": .55, "center_y": .7}

    MDTextField:
        id: getpath
        hint_text: "Include file name with its extension"
        pos_hint: {"center_x": .5, "center_y": 0.7}
        size_hint_x: None
        width: "300dp"

    MDRaisedButton:
        text: "Check Text"
        color: "green"
        pos_hint: {"center_x": .5, "center_y": .45}
        on_release: app.checkprint()

"""

以及您的主要python脚本:

代码语言:javascript
复制
class OneApp(MDApp):

    def build(self):
        self.theme_cls.primary_palette = 'Green'
        self.screen = Builder.load_string(KV)
    

        return self.screen

    
    def checkprint(self):
        value = self.screen.ids.getpath.text
        print("What you typed is: ", value)

OneApp().run()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51177894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档