1
我想讓textinput小部件將文本傳入回調函數,該函數在由printbutton調用時使文本產生標籤,在您考慮時應該相當簡單。但我有一種不看樹木的習慣。安美居,如果任何人都可以算出來,然後它編寫了:P將textinput中的文本傳遞給Kivy中的標籤
import kivy
kivy.require('1.5.1')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class kivyentrywidget(GridLayout):
def __init__(self, **kwargs):
super(kivyentrywidget, self).__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text='What do you want to print?'))
self.text_input = TextInput(multiline=False)
self.add_widget(self.text_input)
self.printbutton = Button(text='Print')
self.printbutton.bind(on_press=callback)
self.add_widget(self.printbutton)
def callback(self):
return Label(text=self.text_input.text)
class Firstapp(App):
def build(self):
return kivyentrywidget()
if __name__ == '__main__':
Firstapp().run()
仍與全球名稱回調沒有定義的錯誤,對不起,非常快的響應來了雖然。 –
請參閱編輯...您需要將它綁定到self.callback不是全局的 –
我應該在回調函數或init中添加它? –