4
Python/Kivy的新手試圖通過一個輸入框,一個ok按鈕和一個應該在單擊ok按鈕時更改文本的標籤來構建一個測試應用程序。但相反,我得到'NameError:全球名稱'txt1'未定義'。我究竟做錯了什麼?在Kivy應用程序中獲取textinput值
# 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()
將這種方法運用工作時千伏語言? – supreme 2017-07-23 22:11:16
在KV中,您應該可以使用: TextInput:\ n text:「」\ n multiline:False – 2017-12-13 17:07:09