1

我试图让 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()
4

1 回答 1

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)

应该解决你的其他问题

于 2013-05-14T18:40:32.080 回答