【问题标题】:Making MDTextField from KivyMD works correct从 KivyMD 制作 MDTextField 工作正常
【发布时间】:2019-09-17 19:14:21
【问题描述】:

大家好,所有遇到过 KivyMD 库的人!

问题是我不能让 MDTextField 像我需要的那样工作。 这就是它应该执行的任务:

  1. 用户在 MDTextField 中输入键,然后按 按钮
  2. 如果密钥正确 - 之后会发生一些事情(例如 - toast("Key is CORRECT!"))
  3. 如果密钥不正确 - 那应该是一个错误(例如 - toast('KEY IS INCORRECT!'))
  4. 如果字符太多(例如 - 超过 5 个),它必须显示一些东西(例如 - toast('Too much text!'))

有main.py:

from kivy.app import App
from kivymd.theming import ThemeManager
from kivymd.label import MDLabel
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.metrics import dp, sp, pt
from kivymd.toast.kivytoast import toast
from kivymd.textfields import MDTextField

class keyinput(MDTextField):
    pass

def toast(text):
    toast(text)

class MyScreen(Screen):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.menu_items = [
                {
                    "viewclass": "MDMenuItem",
                    "text": "text%d" % i,
                    "callback": self.callback,
                }
                for i in range(1, 3)
            ]

        self.menu_button = None

    def change_variable(self, value):
        print("\nvalue=", value)
        self.VARIABLE = value
        print("\tself.VARIABLE=", self.VARIABLE)

    def callback(self, *args):
        toast(args[0])

class MainApp(App):
    title = "KivyMD MDDropdownMenu Demo"
    theme_cls = ThemeManager()

    def build(self):
        return MyScreen()

    def keycheck(self):
        if keyinput.text == '12345':
            toast('KEY IS CORRECT')
        elif len(keyinput.text) > 5:
            toast('Too much text!')
        else:
            toast('KEY IS INCORRECT!')

if __name__ == "__main__":
    MainApp().run()

有main.kv:

#:import MDDropdownMenu kivymd.menus.MDDropdownMenu
#:import MDRaisedButton kivymd.button.MDRaisedButton
#:import MDLabel kivymd.label.MDLabel

<OptionalLabel@MDLabel>:
    halign: 'center'
    font_size: dp(12)

<MDRB@MDRaisedButton>:
    size_hint: None, None
    size: 3 * dp(48), dp(48)
    pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    opposite_colors: True

<keyinput>:
    size_hint_x: 0.5
    halign: 'center'
    pos_hint: {'center_x': .5, 'center_y': .5}
    max_text_length: 5

<MDMenuItem>:
    on_release:
        app.root.change_variable(self.text)
        app.root.menu_button.text = self.text

<MyScreen>:
    name: 'myscrn'
    AnchorLayout:
        anchor_y: 'center'
        BoxLayout:
            orientation: 'vertical'
            size_hint: 0.5, 0.5
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
            spacing: dp(10)
            MDRB:
                text: 'check the key'
                on_release:
                    app.keycheck()
            keyinput:
                hint_text: "print the key here"

【问题讨论】:

标签: python android python-3.x kivy kivy-language


【解决方案1】:

您需要为您的 keyinput 小部件分配一个 id。现在看起来像 在您的 keycheck 功能中,它可能不知道什么 keyinput 方法。 kivy.org/doc/stable/guide/lang.html#reference-widgets

Erik

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 2021-06-16
    • 2017-01-20
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多