Save text input to a variable in a kivy app

安稳与你 提交于 2019-12-24 08:23:22

问题


I am making a text based game, and there is a point at which the game asks the user to input their surname. I have worked out a way to save the name to a file, and load the name from the file, but I do not know how to save the text that has been input into a variable. I have tried various methods i have seen online, but none have worked for me so far. The section of my code in question currently looks like this: (ignore the odd names like customwidget, i was experimenting once and left them like that :P)

testing.py file:

import kivy
kivy.require("1.9.0")
from kivy.properties import NumericProperty

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty

class CustomWidget(Widget):
    last_name_text_input = ObjectProperty()
    ego = NumericProperty(0)
    surname = ''

    def submit_surname(self):
        surname = self.last_name_text_input.text

class CustomWidgetApp(App):
    def build(self):
        return CustomWidget()

customWidget = CustomWidgetApp()
customWidget.run()

customwidget.kv file:

<CustomWidget>:
    last_name_text_input: last_name
    Label:
        text: "Last Name:"
        pos: 655,400
        size: 100, 30
    TextInput:
        id: last_name
        pos: 760,400
        size: 100, 30
    Button:
        text: "Save Name"
        pos: 870,400
        size: 100, 30
        on_release: root.submit_surname()

This creates a screen like this:

However, whenever I save the surname value to the file or try to print surname, it comes up with nothing. It would be greatly appreciated if I could recieve some help with this issue. Thanks in advance for any help :)


回答1:


You have to declare surname as StringProperty. Please refer to the example below.

main.py

    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.properties import ObjectProperty, NumericProperty, StringProperty


    class CustomWidget(Widget):
        last_name_text_input = ObjectProperty()
        ego = NumericProperty(0)
        surname = StringProperty('')

        def submit_surname(self):
            self.surname = self.last_name_text_input.text
            print("Assign surname: {}".format(self.surname))
            self.save()
            self.surname = ''
            print("Reset surname: {}".format(self.surname))
            self.load()
            print("Loaded surname: {}".format(self.surname))

        def save(self):
            with open("surname.txt", "w") as fobj:
                fobj.write(str(self.surname))

        def load(self):
            with open("surname.txt") as fobj:
                for surname in fobj:
                    self.surname = surname.rstrip()


    class CustomWidgetApp(App):
        def build(self):
            return CustomWidget()

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

customwidget.kv

#:kivy 1.10.0

<CustomWidget>:
    last_name_text_input: last_name
    Label:
        text: "Last Name:"
        pos: 655,400
        size: 100, 30
    TextInput:
        id: last_name
        pos: 760,400
        size: 100, 30
    Button:
        text: "Save Name"
        pos: 870,400
        size: 100, 30
        on_release: root.submit_surname()

Output



来源:https://stackoverflow.com/questions/46045092/save-text-input-to-a-variable-in-a-kivy-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!