如何使文本输入框与Kivy一起显示? [英] How to get a text input box to display with Kivy?

查看:358
本文介绍了如何使文本输入框与Kivy一起显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用Kivy编写一个用于学校项目的应用程序(我非常想了解Kivy的知识).我已经为文本输入编写了kv代码,您可以在下面看到它:

I'm currently writing an app in Kivy for a school project (I have very much had to jump in the deep end with Kivy). I have written the kv code for the text input, which you can see below:

 AnswerInput:
 <AnswerInput@BoxLayout>:
    orientation: "vertical"
    BoxLayout:
        height: "40dp"
        size_hint_y: None
        TextInput:
            size_hint_x: 20
        Button:
            text: "Check Answer"
            size_hint_x: 25

我现在需要获取文本框以显示在Python文件中;但是,我对如何执行此操作感到茫然?我的Python代码如下:

I now need to get the text box to display in the Python file; however, I am at something of a loss as to how I would do this? My Python code is below:

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

class TextInputTest(App):

    def __init__(self, *args, **kwargs):
    return TextInput

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

我几乎可以肯定我在这里遗漏了一些东西,可能很简单,但是我是Kivy的初学者.如果有人能让我走上正确的道路,我将不胜感激.

I am almost certain that I am missing something here, probably something very simple, but I am very much a beginner with Kivy. If anyone could put me on the right track, I would be very grateful.

推荐答案

首先,尚不清楚,但是您需要将代码分成py文件和kv文件.看来您已经完成了此操作.您的kv文件也必须全部小写

Firstly, this isn't clear but you need to seperate your code into a py file and a kv file. It seems like you've done this already. Your kv file also needs to be all lowercase

然后在py文件中,为kivy小部件添加一个类.在这种情况下:

In your py file, you then add a class for the kivy widget. In this case:

from kivy.uix.boxlayout import BoxLayout

class AnswerInput(BoxLayout):
    pass

然后在您的kv文件中:

Then in your kv file:

 <AnswerInput>:
    orientation: "vertical"
    BoxLayout:
        height: "40dp"
        size_hint_y: None
        TextInput:
            size_hint_x: 20
        Button:
            text: "Check Answer"
            size_hint_x: 25

您的py中的AnswerInput会查看您已加载的kv文件,以查看是否存在一个与自身名称相同的根窗口小部件.

AnswerInput from your py looks into your loaded kv file to see if there is a root widget with the same name as itself.

(RootWidget表示<>中包含的一堆kv逻辑的顶部小部件)

(RootWidget meaning the top widget of a bunch of kv logic encased in <>)

但是,您必须首先知道如何加载kv文件,有两种方法可以执行此操作.如果您只使用一个kv文件,则可以将您的应用命名为与kv文件相同的名称.

You have to however first know how to load a kv file, there are two ways to do this. If you're using just one kv file, you can name your app the same as your kv file.

所以,如果您的kv文件是

So if your kv file is

textinputtest.kv

您在py中的应用程序类将读取

Your app class in py would read

TextInputTest(App): 
or 
TextInputTestApp(App):

您不需要这样做,也可以使用builder模块来加载文件本身(实际上,如果您有多个kv文件,则需要这样做).

You don't need to do this, you can also use the builder module to load the file itself (and in fact you will need to do this if you have more than one kv file).

要执行此操作,请在py文件中执行此操作:

To do this, you do this in your py file:

from kivy.lang.builder import Builder

Builder.load_file('textinputtest.kv')

您还将返回textinput类的对象,您想要做的是返回自定义textinput类的对象.

You're also returning an object of the textinput class, what you want to do is return an object of your customized textinput class.

您的Py文件​​如下所示:

Your Py file would look like this:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

class AnswerInput(BoxLayout):
    pass

class TextInputTest(App): # If your kv file is called textinputtest.kv

    def build(self):
        return AnswerInput()

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

或者您可以为应用命名任意名称,然后使用构建器将相关的kv文件直接加载到您的应用中.

Or you can name your app anything you want and then use builder to load the relevant kv file directly into your app.

这篇关于如何使文本输入框与Kivy一起显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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