【问题标题】:Reading a text input in kivy在 kivy 中读取文本输入
【发布时间】:2021-11-03 12:48:16
【问题描述】:

大家好,我目前正在做一个项目。这是一个需要登录页面才能启动的国际象棋游戏。目前,我已经让登录页面正常工作并格式化到我很满意的程度,但是由于 kivys 系统,我正在努力阅读用户输入的用户名和密码。

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.core.window import Window
#inport all kivy dependencies without install entire package

def callback(instance):
    print('The button is pressed')
def readUserInfo(instance):
    print(MyGrid..name.text)

class MyGrid(GridLayout):
    def __init__(self, **kwargs):
        Window.size = (300, 120)
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 2
        self.row_force_default=True,
        self.row_default_height=80


        self.add_widget(Label(text="Username: ", size_hint_x=None, width=300))
        self.name = TextInput(multiline=False, size_hint_x=None, width=300, font_size = 50)
        self.add_widget(self.name)

        self.add_widget(Label(text="Password: ", size_hint_x=None, width=300))
        self.lastName = TextInput(multiline=False, password = True, size_hint_x=None, width=300, font_size = 50)
        self.add_widget(self.lastName)

        self.add_widget(Button(text='Register', on_press=callback, size_hint_x=None, width=300, background_color =(99,180,207, 0.8)))
        self.add_widget(Button(text='Login', on_press=readUserInfo , size_hint_x=None, width=300))



class MyApp(App):
    def build(self):
        return MyGrid()


if __name__ == "__main__":
    MyApp().run()```

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    您可以通过访问 TextInput 的text 属性来获取用户在 TextInput 中的输入。

    例如self.name = TextInput(multiline=False, size_hint_x=None, width=300, font_size = 50)

    您可以通过调用self.name.text 来获取用户对该TextInput 的输入

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 2016-06-07
      • 2019-12-11
      • 1970-01-01
      相关资源
      最近更新 更多