<small id='5COc1'></small><noframes id='5COc1'>

    <tfoot id='5COc1'></tfoot>

    • <bdo id='5COc1'></bdo><ul id='5COc1'></ul>

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

  1. <legend id='5COc1'><style id='5COc1'><dir id='5COc1'><q id='5COc1'></q></dir></style></legend>

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

      时间:2023-10-09

    2. <legend id='YGy9m'><style id='YGy9m'><dir id='YGy9m'><q id='YGy9m'></q></dir></style></legend>

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

      • <bdo id='YGy9m'></bdo><ul id='YGy9m'></ul>
          <tfoot id='YGy9m'></tfoot>

              <i id='YGy9m'><tr id='YGy9m'><dt id='YGy9m'><q id='YGy9m'><span id='YGy9m'><b id='YGy9m'><form id='YGy9m'><ins id='YGy9m'></ins><ul id='YGy9m'></ul><sub id='YGy9m'></sub></form><legend id='YGy9m'></legend><bdo id='YGy9m'><pre id='YGy9m'><center id='YGy9m'></center></pre></bdo></b><th id='YGy9m'></th></span></q></dt></tr></i><div id='YGy9m'><tfoot id='YGy9m'></tfoot><dl id='YGy9m'><fieldset id='YGy9m'></fieldset></dl></div>
                  <tbody id='YGy9m'></tbody>
                本文介绍了在 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 应用程序中获取文本输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何从线程更新 Kivy 元素? 下一篇:有没有一种简单的方法可以在没有图像的情况下

                相关文章

                最新文章

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

                1. <small id='K8iN8'></small><noframes id='K8iN8'>

                  <legend id='K8iN8'><style id='K8iN8'><dir id='K8iN8'><q id='K8iN8'></q></dir></style></legend>
                  <tfoot id='K8iN8'></tfoot>

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