• <legend id='PuMFP'><style id='PuMFP'><dir id='PuMFP'><q id='PuMFP'></q></dir></style></legend>

    1. <tfoot id='PuMFP'></tfoot>

      1. <i id='PuMFP'><tr id='PuMFP'><dt id='PuMFP'><q id='PuMFP'><span id='PuMFP'><b id='PuMFP'><form id='PuMFP'><ins id='PuMFP'></ins><ul id='PuMFP'></ul><sub id='PuMFP'></sub></form><legend id='PuMFP'></legend><bdo id='PuMFP'><pre id='PuMFP'><center id='PuMFP'></center></pre></bdo></b><th id='PuMFP'></th></span></q></dt></tr></i><div id='PuMFP'><tfoot id='PuMFP'></tfoot><dl id='PuMFP'><fieldset id='PuMFP'></fieldset></dl></div>
      2. <small id='PuMFP'></small><noframes id='PuMFP'>

          <bdo id='PuMFP'></bdo><ul id='PuMFP'></ul>

        将文本输入保存到 kivy 应用程序中的变量

        时间:2023-10-09
          <tbody id='DHLlr'></tbody>

              <i id='DHLlr'><tr id='DHLlr'><dt id='DHLlr'><q id='DHLlr'><span id='DHLlr'><b id='DHLlr'><form id='DHLlr'><ins id='DHLlr'></ins><ul id='DHLlr'></ul><sub id='DHLlr'></sub></form><legend id='DHLlr'></legend><bdo id='DHLlr'><pre id='DHLlr'><center id='DHLlr'></center></pre></bdo></b><th id='DHLlr'></th></span></q></dt></tr></i><div id='DHLlr'><tfoot id='DHLlr'></tfoot><dl id='DHLlr'><fieldset id='DHLlr'></fieldset></dl></div>

              <small id='DHLlr'></small><noframes id='DHLlr'>

              <tfoot id='DHLlr'></tfoot>

              • <legend id='DHLlr'><style id='DHLlr'><dir id='DHLlr'><q id='DHLlr'></q></dir></style></legend>

                • <bdo id='DHLlr'></bdo><ul id='DHLlr'></ul>
                  本文介绍了将文本输入保存到 kivy 应用程序中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在制作一个基于文本的游戏,游戏会在某个时刻要求用户输入他们的姓氏.我已经找到了一种将名称保存到文件中并从文件中加载名称的方法,但我不知道如何将已输入的文本保存到变量中.我已经尝试了我在网上看到的各种方法,但到目前为止没有一个对我有用.我有问题的代码部分目前看起来像这样:(忽略像customwidget这样的奇怪名称,我曾经尝试过一次,然后就这样离开了:P)

                  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 文件:

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

                  这会创建一个像这样的屏幕:

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

                  推荐答案

                  你必须将 surname 声明为 StringProperty.请参考以下示例.

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

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

                  输出

                  这篇关于将文本输入保存到 kivy 应用程序中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Scatter 大小和位置的问题 下一篇:Python kivy 入口点 inflateRest2 无法定位 libpng16-16.d

                  相关文章

                  最新文章

                  <tfoot id='k5PiA'></tfoot>

                • <small id='k5PiA'></small><noframes id='k5PiA'>

                  <legend id='k5PiA'><style id='k5PiA'><dir id='k5PiA'><q id='k5PiA'></q></dir></style></legend>

                    1. <i id='k5PiA'><tr id='k5PiA'><dt id='k5PiA'><q id='k5PiA'><span id='k5PiA'><b id='k5PiA'><form id='k5PiA'><ins id='k5PiA'></ins><ul id='k5PiA'></ul><sub id='k5PiA'></sub></form><legend id='k5PiA'></legend><bdo id='k5PiA'><pre id='k5PiA'><center id='k5PiA'></center></pre></bdo></b><th id='k5PiA'></th></span></q></dt></tr></i><div id='k5PiA'><tfoot id='k5PiA'></tfoot><dl id='k5PiA'><fieldset id='k5PiA'></fieldset></dl></div>

                        <bdo id='k5PiA'></bdo><ul id='k5PiA'></ul>