简体   繁体   English

我想将文本字段 in.kv 文件中的文本加载到变量 in.py 文件中

[英]I want to load text from text field in .kv file to a variable in .py file

I want to be able to click on MDFlatButton that says Štart and i want it to let's say for an example to call get_user_input(self) which would print whatever is inside text field.我希望能够单击显示 Štart 的 MDFlatButton,我希望它以调用 get_user_input(self) 为例,它将打印文本字段内的任何内容。 And I have been struggling with this for 2 whole days and i have no idea what to do, I am just a beginner and I have no clue what am I doing so sorry if it's messy.我已经为此苦苦挣扎了整整 2 天,我不知道该怎么做,我只是一个初学者,我不知道我在做什么,如果它很乱,我很抱歉。 Ty for help, those are my files: main.py file:求助,这些是我的文件:main.py 文件:


from kivy.core.window import Window
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.textfield import MDTextField
from kivy.properties import StringProperty

class testy(Screen):
    novy_test = ObjectProperty()


class Test(MDApp):

    Window.size = (1170 / 3, 2532 / 3)
    # input_number = ObjectProperty()
    def build(self):
        self.theme_cls.material_style = "M3"
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = 'Gray'


        return testy()
    def get_user_input(self):
        print(self.root.ids.my_textfield_1.ids.user.text)
    def callback(self, button):
        pass



class CustomOverFlowMenu(MDDropdownMenu):
    # In this class you can set custom properties for the overflow menu.
    pass

Test().run()


test.kv file:测试.kv 文件:

#:import Factory kivy.factory.Factory

#:import CustomOverFlowMenu __main__.CustomOverFlowMenu

<testy>:
    name:'testy'
    id: testy
    MDBoxLayout:
        orientation: "vertical"

        MDTopAppBar:
            title: "MDTopAppBar"
            use_overflow: True
            overflow_cls: CustomOverFlowMenu()
            specific_text_color: .51,.51,.51,1
            md_bg_color: 0, 0, 0, 1



            left_action_items: [["car", lambda x: Factory.novy_test().open(), '',"Nový test"]]


        MDBottomNavigation:
            panel_color: "black"
            selected_color_background: "white"
            text_color_active: "lightgray"
            selected_color_background: 1, 1, 1, .4

            MDBottomNavigationItem:
                name: 'screen 1'
                text: 'Testy'
                icon: 'road-variant'

                MDLabel:
                    text: 'Test'
                    halign: 'center'


            MDBottomNavigationItem:
                name: 'screen 2'
                text: 'chyby'
                icon: 'alert-circle'


                MDLabel:
                    text: 'Chyby'
                    halign: 'center'

            MDBottomNavigationItem:
                name: 'screen 3'
                text: 'Settings'
                icon: 'cog'

                MDLabel:
                    text: 'LinkedIN'
                    halign: 'center'
<novy_test@Popup>:
    id:my_textfield_1
    size_hint: .8, .45
    title: 'Nový test'
    separator_color: 'black'

    title_align: 'center'
    BoxLayout:
        id: layout

        spacing: 10
        orientation:'vertical'

        MDTextField:
            id: user
            hint_text: "Číslo testu"
            mode: "round"
            pos_hint: {"top": 1}

        MDFlatButton:
            text: 'Štart'
            pos_hint: {'center_x': .5}
            on_press: app.get_user_input()


        MDFlatButton:
            pos_hint: {'center_x': .5}
            text:'test z nesprávnych'
        MDFlatButton:
            text:'test z neurobených'
            pos_hint: {'center_x': .5}
        MDFlatButton:
            text:'test z neurobených'
            pos_hint: {'center_x': .5}
        MDFlatButton:
            text:'test z neurobených'
            pos_hint: {'center_x': .5}


I didn't perfectly understand all that you wanted to do, but in this example, the text a person types into the box will print.我并不完全理解您想做的所有事情,但在这个例子中,将打印一个人在框中键入的文本。 I moved the.kv language into a string just for convenience of creating a one-file runnable project.为了方便创建单文件可运行项目,我将 .kv 语言移动到一个字符串中。 I tested it.我测试了它。

The main point is this code, inside the.kv file/kivy language you can use the id property to get a reference to the objects, and then the.text property of that object is sent as an argument to the function.重点是这段代码,在 .kv 文件/kivy 语言中,你可以使用 id 属性来获取对对象的引用,然后将 object 的 .text 属性作为参数发送给 function。

on_press: app.get_user_input(user.text)

runnable:可运行:

from kivy.core.window import Window
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.menu import MDDropdownMenu
from kivy.uix.popup import Popup
from kivymd.uix.textfield import MDTextField
from kivy.properties import StringProperty

Builder.load_string('''
#:import Factory kivy.factory.Factory

<testy>:
    name:'testy'
    id: testy
    MDBoxLayout:
        orientation: "vertical"
        MDTopAppBar:
            title: "MDTopAppBar"
            use_overflow: True
            # overflow_cls: CustomOverFlowMenu()
            specific_text_color: .51,.51,.51,1
            md_bg_color: 0, 0, 0, 1
            left_action_items: [["car", lambda x: Factory.NovyTest().open(), '',"Nový test"]]
        MDBottomNavigation:
            panel_color: "black"
            selected_color_background: "white"
            text_color_active: "lightgray"
            selected_color_background: 1, 1, 1, .4
            MDBottomNavigationItem:
                name: 'screen 1'
                text: 'Testy'
                icon: 'road-variant'
                MDLabel:
                    text: 'Test'
                    halign: 'center'
            MDBottomNavigationItem:
                name: 'screen 2'
                text: 'chyby'
                icon: 'alert-circle'
                MDLabel:
                    text: 'Chyby'
                    halign: 'center'
            MDBottomNavigationItem:
                name: 'screen 3'
                text: 'Settings'
                icon: 'cog'
                MDLabel:
                    text: 'LinkedIN'
                    halign: 'center'
<NovyTest@Popup>:
    id:my_textfield_1
    size_hint: .8, .45
    title: 'Nový test'
    separator_color: 'black'
    title_align: 'center'
    BoxLayout:
        id: layout
        spacing: 10
        orientation:'vertical'
        MDTextField:
            id: user
            hint_text: "Číslo testu"
            mode: "round"
            pos_hint: {"top": 1}
        MDFlatButton:
            text: 'Štart'
            pos_hint: {'center_x': .5}
            # inside .kv/kivy language you can use the id property
            on_press: app.get_user_input(user.text)
        MDFlatButton:
            pos_hint: {'center_x': .5}
            text:'test z nesprávnych'
        MDFlatButton:
            text:'test z neurobených'
            pos_hint: {'center_x': .5}
        MDFlatButton:
            text:'test z neurobených'
            pos_hint: {'center_x': .5}
        MDFlatButton:
            text:'test z neurobených'
            pos_hint: {'center_x': .5}
'''
)


class NovyTest(Popup):
    # just an example, not used in this code
    def __init__(self, **kw):
        super().__init__(**kw)


class testy(Screen):
    # can list objects defined in .kv file and give them a type hint corresponding to object type
    novy_test: NovyTest

    def __init__(self, **kw):
        super().__init__(**kw)
        print("creating screen testy")

    # novy_test = ObjectProperty()


class Test(MDApp):

    Window.size = (1170 / 3, 2532 / 3)
    # input_number = ObjectProperty()

    def build(self) -> testy:
        self.theme_cls.material_style = "M3"
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = 'Gray'
        return testy()

    def get_user_input(self, input_text, *args):
        print(f"{self} user input: {input_text}")
        # print(self.root.ids.my_textfield_1.ids.user.text)

    def callback(self, button):
        pass


class CustomOverFlowMenu(MDDropdownMenu):
    # In this class you can set custom properties for the overflow menu.
    pass


Test().run()

end结尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM