如何在kivyMD中获取标签的文本输入

Posted

技术标签:

【中文标题】如何在kivyMD中获取标签的文本输入【英文标题】:How to get the text input of a label in kivyMD 【发布时间】:2021-08-24 22:05:03 【问题描述】:

我正在使用 KivyMD,我正在尝试从 kivyMD 中的文本输入中获取文本。 我不断收到以下错误:

[INFO   ] [Logger      ] Record log in C:\Users\Gavin\.kivy\logs\kivy_21-06-07_61.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.gstreamer" 0.3.2
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.3.1
[INFO   ] [Kivy        ] v2.0.0
[INFO   ] [Kivy        ] Installed at "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\python.exe"
[INFO   ] [KivyMD      ] 1.0.0.dev0, git-d99a3d0, 2021-06-07 (installed at "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivymd\__init__.py")
[INFO   ] [Factory     ] 186 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.0.0 - Build 10.18.10.5069'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) HD Graphics 4000'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 0
[INFO   ] [GL          ] Shading version <b'4.00 - Build 10.18.10.5069'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <16>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Start application main loop
[INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "c:\Users\Gavin\Documents\GitHub\Mobile Temperture Convertor\Mobile-Temperature-Convertor\test.py", line 30, in <module>
     DemoApp().run()
   File "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\app.py", line 950, in run
     runTouchApp()
   File "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 582, in runTouchApp
     EventLoop.mainloop()
   File "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 347, in mainloop
     self.idle()
   File "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 391, in idle
     self.dispatch_input()
   File "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 342, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 308, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivymd\uix\behaviors\ripple_behavior.py", line 296, in on_touch_up
     return super().on_touch_up(touch)
   File "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivymd\uix\button.py", line 981, in on_touch_up
     return super().on_touch_up(touch)
   File "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
     self.dispatch('on_release')
   File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1132, in kivy._event.EventObservers._dispatch
   File "C:\Users\Gavin\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\builder.py", line 57, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "C:\Users\Gavin\Documents\GitHub\Mobile Temperture Convertor\Mobile-Temperature-Convertor\test.kv", line 19, in <module>
     on_release: self.show_data
   File "kivy\weakproxy.pyx", line 32, in kivy.weakproxy.WeakProxy.__getattr__
 AttributeError: 'MDRoundFlatButton' object has no attribute 'show_data'

这里是python文件:

from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.button import MDRectangleFlatButton, MDRoundFlatButton
from kivymd.uix.textfield import MDTextField
from kivy.lang import Builder






    class DemoApp(MDApp):
        
        def show_data(self):
            inputFahrenheit = self.root.ids.fahrenheit_input.text
            print(inputFahrenheit)
    
    
        def build(self):
            kv = Builder.load_file("test.kv") 
            self.theme_cls.primary_palette = "Green"
            self.theme_cls.primary_hue = "A700"
            self.theme_cls.theme_style = "Light"
            screen = Screen()
            
            return kv
            
    
    
    DemoApp().run()

这是 .kv 文件:

Screen:

    MDTextField:
        id: fahrenheit
        hint_text:"Enter Fahrenheit"
        helper_text: "Once you enter the fahrenheit the press submit"
        helper_text_mode: "on_focus"
        icon_right: "temperature-fahrenheit"
        pos_hint: 'center_x': 0.5, 'center_y': 0.9
        size: 200, 25
        size_hint: None, None

            
    MDRoundFlatButton:
        text: "Enter"
        pos_hint: 'center_x': 0.5, 'center_y': 0.2
        text_color: 0, 1, 0, 1
        size_hint: 0.25, 0.20
        on_release: self.show_data

不知道出了什么问题希望我能得到社区的帮助。 我以前在正常的kivy中做过这个,所以我不知道发生了什么。

EDIT 以显示代码在@John Andersons 尝试提供帮助但代码不起作用后的当前状态。

Python 文件:

    from kivymd.app import MDApp
    from kivymd.uix.screen import Screen
    from kivymd.uix.button import MDRectangleFlatButton, MDRoundFlatButton
    from kivymd.uix.textfield import MDTextField
    from kivy.lang import Builder
    
    
    
    
    
    
    class DemoApp(MDApp):
            
        def show_data(self):
            inputFahrenheit = self.root.ids.fahrenheit.text
            print(inputFahrenheit)
        
        
        def build(self):
            kv = Builder.load_file("test.kv") 
            self.theme_cls.primary_palette = "Green"
            self.theme_cls.primary_hue = "A700"
            self.theme_cls.theme_style = "Light"
            screen = Screen()
                
            return kv
                
        
    DemoApp().run()

KV 文件:

Screen:

    MDTextField:
        id: fahrenheit
        hint_text:"Enter Fahrenheit"
        helper_text: "Once you enter the fahrenheit the press submit"
        helper_text_mode: "on_focus"
        icon_right: "temperature-fahrenheit"
        pos_hint: 'center_x': 0.5, 'center_y': 0.9
        size: 200, 25
        size_hint: None, None

            
    MDRoundFlatButton:
        text: "Enter"
        pos_hint: 'center_x': 0.5, 'center_y': 0.2
        text_color: 0, 1, 0, 1
        size_hint: 0.25, 0.20
        on_release: app.show_data


   

【问题讨论】:

【参考方案1】:

如错误消息所述:

'MDRoundFlatButton' object has no attribute 'show_data'

在你的kv

on_release: self.show_data

selfMDRoundFlatButton。由于您的 show_data() 方法在您的 DemoApp 中,请更改 kv 以使用该方法:

on_release: app.show_data()

app 是对DemoApp 的引用。另请注意,在 kv 中,() 是必需的。

您在kv 中定义的idfahrenheit,但您正在尝试访问fahrenheit_input。切换一个,使它们匹配。

【讨论】:

您的回答阻止了应用程序崩溃,但我在终端中没有得到任何输出。 您的代码仍然无法正常工作,我已在问题中进行了编辑以查看代码的当前状态。 您仍然在on_release: app.show_data() 中省略了所需的() 如果你给这个问题一个赞成票,我将不胜感激,以便其他人可以得到一些帮助。

以上是关于如何在kivyMD中获取标签的文本输入的主要内容,如果未能解决你的问题,请参考以下文章

KivyMD:如何从 python 中添加的 TextField 获取文本

如何在kivymd focus agane中制作输入字段

如何在 kivymd 中添加多个屏幕?

如何从 KivyMD 文本字段中获取文本?

如何从分配给 KivyMD Python 中的 MDLists 的 TextFields 获取数据

如何在 kivymd 文本字段上设置两种模式?