在Kivy中将文本从textinput传递到label



我试图让textput小部件将文本传递到回调函数中,该回调函数在由printbutton调用时使用文本生成标签,当您考虑它时应该相当简单。但我有只见树木不见森林的习惯。无论如何,如果有人能弄清楚,那就把它编码出来: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()
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)

应该能解决你的其他问题

相关内容

  • 没有找到相关文章

最新更新