【问题标题】:Reference a input box id on kivy screen在 kivy 屏幕上引用输入框 id
【发布时间】:2021-04-10 03:15:31
【问题描述】:

我认为这个问题很容易解决,但我没有足够的经验来解决这个问题。 因此,我想为 press() 函数内的变量“name”引用输入文本(kv 文件上的 MDTextField),以便我可以在弹出窗口中写入。 我正在为大学项目学习 kivymd,因此,请忽略错误和不必要的信息:/

这是我的 kv 文件

Screen:
    id: screen1
    NavigationLayout:
        id: navigation
        ScreenManager:
            id: screen_manager
            Screen:
                id: fact
                name: 'fact'
                BoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "Menu"
                        elevation: 10
                        left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
                    Widget:
                MDLabel:
                    text: "Fact Checker"
                    font_size: 56
                    font_name: "MontserratBold"
                    pos_hint: {'center_x': 0.75, 'center_y': 0.7}
                    theme_text_color: "Custom"
                    text_color: (53/255, 183/255, 1, 1)

                MDTextField:
                    id: input_box
                    hint_text: "Type here"
                    font_size: 18
                    pos_hint: {'center_x': 0.5, 'center_y': 0.45}
                    size_hint_x: None
                    width: 500
                    multiline: True
                    helper_text: "Type the title of the news"
                    helper_text_mode: "on_focus"
                    theme_text_color: "Custom"
                    text_color: (53/255, 183/255, 1, 1)
                    line_color_normal: 1, 1, 1, 1

                MDRectangleFlatButton:
                    text: "Check"
                    pos_hint: {'center_x': 0.5, 'center_y': 0.25}
                    theme_text_color: "Custom"
                    text_color: (1, 1, 1, 1)
                    on_press: app.press()

                MDLabel:
                    markup: True
                    text: "<Developed by: [color=#cfd0d1]Caio Barreto and Gabriel Queiroz[/color]>"
                    font_name: "Montserrat"
                    theme_text_color: "Custom"
                    text_color: (53/255, 183/255, 1, 0.5)
                    pos_hint: {'center_x': 0.74, 'center_y': 0.1}
            Screen:
                name: 'discord'
                BoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "Menu"
                        elevation: 10
                        left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
                    Widget:
                MDLabel:
                    text: 'teste1'

            Screen:
                name: 'add'
                BoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "Menu"
                        elevation: 10
                        left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
                    Widget:
                MDLabel:
                    text: 'teste2'

        MDNavigationDrawer:
            id: nav_drawer
            ContentNavigationDrawer:
                ScrollView:
                    DrawerList:
                        id: md_list

                        MDList:
                            OneLineIconListItem:
                                text: "Menu"
                                on_press: screen_manager.current = "fact"
                                IconLeftWidget:
                                    icon: "home"

                            OneLineIconListItem:
                                text: "Discord"
                                on_press: screen_manager.current = "discord"
                                IconLeftWidget:
                                    icon: "discord"


                            OneLineIconListItem:
                                text: "Add more news"
                                on_press: screen_manager.current = "add"
                                IconLeftWidget:
                                    icon: "circle-edit-outline"

这是我的 python 代码:

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivymd.uix.dialog import MDDialog
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDRectangleFlatButton, MDFlatButton
from kivymd.uix.navigationdrawer import NavigationLayout
from kivymd.uix.list import MDList
from kivymd.uix.boxlayout import BoxLayout
from kivymd.theming import ThemableBehavior
from kivy.core.text import LabelBase
from kivy.uix.widget import Widget
from helper import username_input
from kivymd.uix.screen import Screen

LabelBase.register(name="MontserratBold", fn_regular="font/Montserrat-Bold.ttf")
LabelBase.register(name="Montserrat", fn_regular="font/Montserrat-Medium.ttf")

class FactCheckerApp(MDApp):

    class ContentNavigationDrawer(BoxLayout):
        pass

    class DrawerList(ThemableBehavior, MDList):
        pass

    def build(self):
        self.file = Builder.load_file('projeto.kv')
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "LightBlue"
        print(self.file.Screen.ids.)
        return self.file

    def press(self):
        name = self.screen1.navigation.screen_manager.fact.input_box.text
        close_button = MDFlatButton(text='Close', on_release=self.close_dialog)
        self.dialog = MDDialog(title='Answer', text=name, size_hint=(0.5, 1), buttons=[close_button])
        self.dialog.open()

    def close_dialog(self, obj):
        self.dialog.dismiss()


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

【问题讨论】:

    标签: python kivy kivy-language kivymd


    【解决方案1】:

    要访问MDTextField,您可以通过它的id 来引用它,该id 是在定义idkv 规则的根小部件中定义的(在这种情况下,在Screen)。尝试改变:

        name = self.screen1.navigation.screen_manager.fact.input_box.text
    

    到:

        name = self.root.ids.input_box.text
    

    press() 方法中。

    【讨论】:

    • 它不起作用,程序在它出现在屏幕上之前就关闭了,代码末尾出现的错误是:File "D:\python\venv\lib\site-packages\kivy \lang\parser.py",第 616 行,在 parse_level current_object.children = _objects AttributeError: 'NoneType' object has no attribute 'children'
    • 该消息表明您的kv 文件中可能存在错误。
    • 我没有收到您在此处发布的 kv 的任何错误,因此请先注释掉更改,直到您消除错误消息。
    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    相关资源
    最近更新 更多