简体   繁体   中英

How to get a text input box to display with Kivy?

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

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()

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.

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

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

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

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 meaning the top widget of a bunch of kv logic encased in <>)

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.

So if your kv file is

textinputtest.kv

Your app class in py would read

TextInputTest(App): 
or 
TextInputTestApp(App):

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).

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

from kivy.lang.builder import Builder

Builder.load_file('textinputtest.kv')

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

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM