【发布时间】:2017-09-04 23:29:19
【问题描述】:
我正在制作一个基于文本的游戏,并且在某个时刻游戏要求用户输入他们的姓氏。我已经找到了一种将名称保存到文件中并从文件中加载名称的方法,但我不知道如何将输入的文本保存到变量中。我已经尝试了我在网上看到的各种方法,但到目前为止没有一个对我有用。我的代码部分目前看起来像这样:(忽略像customwidget这样的奇怪名称,我曾经尝试过一次,然后就这样离开了:P)
testing.py 文件:
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 文件:
<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()
这会创建一个像这样的屏幕:
但是,每当我将姓氏值保存到文件或尝试打印姓氏时,它什么都没有出现。如果我能在这个问题上得到一些帮助,将不胜感激。提前感谢您的帮助:)
【问题讨论】:
-
这对我来说很好,你是如何尝试打印/保存到文件的?
标签: python python-3.x kivy python-3.6