Kivy入门:使用Kivy获取用户输入 [英] Getting started with Kivy: getting user input using Kivy

查看:305
本文介绍了Kivy入门:使用Kivy获取用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Kivy开始,因为我是用Python编写代码的,但是我发现这真的很难!您能否对它的工作原理做出很好的解释? 例如,即使这看起来也很模糊(这是他们网站的第二个示例).

I would like to start with Kivy since I code in Python but I find it really hard! Can you lead to a good explanation of how it works? For example even this looks quite foggy (that is the second example of their website).

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput


class LoginScreen(GridLayout):

    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        self.cols = 2
        self.add_widget(Label(text='User Name'))
        self.username = TextInput(multiline=False)
        self.add_widget(self.username)
        self.add_widget(Label(text='password'))
        self.password = TextInput(password=True, multiline=False)
        self.add_widget(self.password)


class MyApp(App):

    def build(self):
        return LoginScreen()


if __name__ == '__main__':
    MyApp().run()

我想从一个基本的应用程序开始,该应用程序要求用户输入并显示从中创建的内容. 例如,在没有任何GUI的基本Python中,它可能是:

I would like to start with a basic App that asks user for input and display something created from it. For example in basic Python without any GUI it could be:

def hex_enc(text_input):
    return text_input.encode('hex')

def hex_dec(text_input):
    return text_input.decode('hex')

while True:
    text_input = raw_input('Input  : ')
    mode = raw_input('Mode   : ').lower()
    if ('encrypt' in mode):
        print hex_enc(text_input)
    else:
        print hex_dec(text_input)

我想我需要一个textinput和一个标签,这将是该textinput的结果. 但这是非常混乱的,我不知道并将其全部用于猕猴桃课!

I think I would need one textinput and one label which would be the result of that textinput. But this is very messy and I don't know and to use all of that into kivy class!

推荐答案

好的.这是一个仅用Python实现的示例,没有.kv文件.我在其中有一些评论,以尝试逐步解释所发生的事情.添加感叹号后,它将弹出一个用户可以在其中键入内容的文本框,然后将其吐回到标签中.您显然可以比添加标点符号做更多有趣的事情,但是我决定使示例保持简单.

Okay. Here's an example implemented solely in Python, without a .kv file. I've got some comments in there to try to explain what's going on step-by-step. It'll pop up a text box that the user can type stuff into, and spit that back out in a label, after adding an exclamation mark. You can obviously do more interesting stuff than adding punctuation, but I decided to keep the example simple.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput


class MyDumbScreen(BoxLayout):  # Changed to a BoxLayout for simplicity

    def __init__(self, **kwargs):
        # Call the base class constructor :)
        # Note that this is where it will pick up on any stuff you did in the
        # .kv file (if you have one)
        super(MyDumbScreen, self).__init__(**kwargs)
        self.orientation = "vertical"

        # Here we are just creating a text input
        my_user_input = TextInput()

        # Here we add it to MyDumbScreen's widget tree.  If you skip this step,
        # you'll never see your precious widget.
        self.add_widget(my_user_input)

        # This is the label that will hold a modified version of the user's
        # input
        my_output = Label(text="initial value")
        self.add_widget(my_output)

        # Here we create a callback function
        # This callback will be called whenever the 'text' property of
        # our TextInput is modified
        def callback(instance, value):
            my_output.text = value + "!"

        # Here we "bind" the callback to the TextInput's 'text' property
        # If you skip this step, you won't see the changes ever take place
        my_user_input.bind(text=callback)


class MyApp(App):

    def build(self):
        return MyDumbScreen()


if __name__ == '__main__':
    MyApp().run()

请注意,我通常认为这是比仅使用.kv文件更复杂的方法.

Note that I typically regard this as being a more complicated approach than simply using a .kv file.

如果您走那条路线,您的课程定义将如下所示:

If you went that route, your class definition would look like this:

class MyDumbScreen(BoxLayout):  # Changed to a BoxLayout for simplicity
    pass

您的.kv文件如下所示:

And your .kv file would look like this:

<MyDumbScreen@BoxLayout>:
    orientation: "vertical"

    TextInput:
        id: my_user_input
        text: ""
    Label:
        id: my_output # This id isn't serving a purpose
        text: my_user_input.text + "!"

使用.kv方法,您无需手动添加所有内容到小部件树中,也无需亲自编写/添加回调.

With the .kv approach, you didn't have all the noise of adding stuff to the widget tree manually, or writing/adding a callback yourself.

请记住,默认情况下,Kivy将根据您的App类的名称和.kv文件的名称加载kv文件,并且区分大小写因操作系统而异.

Remember that, by default, Kivy will load the kv file based on the name of your App class and the name of the .kv file, and that case sensitivity varies by os.

这篇关于Kivy入门:使用Kivy获取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆