2013-05-14 69 views
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() 

回答

2
def callback(self,evt=None): #not sure if kivy sends event info so added optional arg just in case 
    return self.add_widget(Label(text=self.text_input.text)) 

也許......不是太熟悉kivy,但我認爲這將做到這一點..

self.printbutton.bind(on_press=self.callback) 

應該解決您的另一個問題

+0

仍與全球名稱回調沒有定義的錯誤,對不起,非常快的響應來了雖然。 –

+0

請參閱編輯...您需要將它綁定到self.callback不是全局的 –

+0

我應該在回調函數或init中添加它? –

相關問題