我试图让textinput小部件将文本传递给回调函数,该函数在printbutton调用时生成带有文本的标签,当你想到它时应该相当简单。但我有一种习惯,就是看不到树木。 Anyhoo,如果有人能解决这个问题,那就编码吧:P
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()
答案 0 :(得分: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)
应解决您的其他问题