首页 > 解决方案 > 如何在kivyMD中获取标签的文本输入

问题描述

我正在使用 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做过这个,所以我不知道发生了什么。

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

蟒蛇文件:

    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


   

标签: pythonpython-3.xkivykivy-languagekivymd

解决方案


正如错误消息所述:

'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()必需的。

id您在您的kvis中定义fahrenheit,但您正在尝试访问fahrenheit_input。Swict 一个,使它们匹配。


推荐阅读