• <bdo id='AUghX'></bdo><ul id='AUghX'></ul>

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

      <legend id='AUghX'><style id='AUghX'><dir id='AUghX'><q id='AUghX'></q></dir></style></legend>
      1. <small id='AUghX'></small><noframes id='AUghX'>

        在 Kivy 应用程序中获取文本输入值

        时间:2023-06-07
          <tbody id='b46xV'></tbody>
            <bdo id='b46xV'></bdo><ul id='b46xV'></ul>
            <legend id='b46xV'><style id='b46xV'><dir id='b46xV'><q id='b46xV'></q></dir></style></legend>
            <i id='b46xV'><tr id='b46xV'><dt id='b46xV'><q id='b46xV'><span id='b46xV'><b id='b46xV'><form id='b46xV'><ins id='b46xV'></ins><ul id='b46xV'></ul><sub id='b46xV'></sub></form><legend id='b46xV'></legend><bdo id='b46xV'><pre id='b46xV'><center id='b46xV'></center></pre></bdo></b><th id='b46xV'></th></span></q></dt></tr></i><div id='b46xV'><tfoot id='b46xV'></tfoot><dl id='b46xV'><fieldset id='b46xV'></fieldset></dl></div>
            <tfoot id='b46xV'></tfoot>

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

                1. 本文介绍了在 Kivy 应用程序中获取文本输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Python/Kivy 的新手尝试构建一个带有输入框、ok 按钮和一个标签的测试应用程序,该标签在单击 ok 按钮时应该更改文本.但相反,我得到'NameError:全局名称'txt1'未定义'.我做错了什么?

                  New to Python/Kivy trying to build a test app with an input box, an ok button and a label that should change text when the ok button is clicked. But instead I get 'NameError: global name 'txt1' is not defined'. What am I doing wrong?

                  # import Kivy
                  import kivy
                  import random
                  
                  from kivy.app import App
                  from kivy.uix.button import Button
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.uix.label import Label
                  from kivy.uix.textinput import TextInput
                  
                  # my app
                  class MyApp(App):
                  # layout
                      def build(self):
                          layout = BoxLayout(padding=10, orientation='vertical')
                          btn1 = Button(text="OK")
                          btn1.bind(on_press=self.buttonClicked)
                          layout.add_widget(btn1)
                          lbl1 = Label(text="test")
                          layout.add_widget(lbl1)
                          txt1 = TextInput(text='', multiline=False)
                          layout.add_widget(txt1)
                          return layout
                  
                  # button click function
                      def buttonClicked(self,btn):
                          lbl1.text = "You wrote " + txt1.text
                  
                  # run app
                  if __name__ == "__main__":
                      MyApp().run()
                  

                  推荐答案

                  使用self.txt1, self.lbl1等..

                  class MyApp(App):
                  # layout
                      def build(self):
                          layout = BoxLayout(padding=10, orientation='vertical')
                          btn1 = Button(text="OK")
                          btn1.bind(on_press=self.buttonClicked)
                          layout.add_widget(btn1)
                          self.lbl1 = Label(text="test")
                          layout.add_widget(self.lbl1)
                          self.txt1 = TextInput(text='', multiline=False)
                          layout.add_widget(self.txt1)
                          return layout
                  
                  # button click function
                      def buttonClicked(self,btn):
                          self.lbl1.text = "You wrote " + self.txt1.text
                  
                  # run app
                  if __name__ == "__main__":
                      MyApp().run()
                   # join all items in a list into 1 big string
                  

                  使用:

                  def buttonClicked(self,btn):
                          lbl1.text = "You wrote " + txt1.text <- only exists in the build method
                  

                  这篇关于在 Kivy 应用程序中获取文本输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何从线程更新 Kivy 元素? 下一篇:有没有一种简单的方法可以在没有图像的情况下为 Kivy 标签、按钮、小部件等添加边框?

                  相关文章

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

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

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

                    <tfoot id='ZHbok'></tfoot>