Kivy文字输入。使用Clock.schedule_once()设置文本

时间:2019-08-10 15:52:46

标签: kivy

尽管我将显示一些文本设置为https并将其显示在屏幕上非常简单,但是看来我错了。在下面的代码中,我需要将文本TextInput设置为文本输入并切换选项卡。我只有在取消注释Lorem ipsum...时才能看到文本。我将使用Clock.schedule_interval(self.set_text, 1)或其他任何方式,而不仅仅是不断调用Clock.schedule_once方法。

set_text()

编辑

我尝试过:

SNIPPET

'''
Test of the widget TabbedPanel.
'''


from kivy.app import App
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader
from kivy.factory import Factory

theRoot = """
#:import Factory kivy.factory.Factory

<EditButton>
    orientation: 'vertical'
    Button:
        text: 'Switch to Edit Screen'
        on_press: root.change_tab()

<EditInput>
    orientation: 'vertical'
    TextInput:
        id: text_input
<UnCloseableHeader>
    color: 0,0,0,1

    disabled_color: self.color
    # variable tab_width
    text: 'tabx'
    size_hint_x: None
    width: self.texture_size[0] + 40
    BoxLayout:
        pos: root.pos
        size_hint: None, None
        size_y: 20
        padding: 3
        Label:
            id: lbl
            text: root.text

<MainTabbedPanel>:
    size_hint: (1, 1)
    do_default_tab: False
    #default_tab: edit_button_tab
    tab_width: 130

    FloatLayout:
        EditButton:
            id: edit_button
        EditInput:
            id: edit_input

    UnCloseableHeader:
        id: edit_button_tab
        text: 'Edit'
        content: edit_button.__self__

    UnCloseableHeader:
        id: edit_input_tab
        text: 'Edit Tab'
        content: edit_input.__self__

MainTabbedPanel:    

"""

class EditInput(BoxLayout):
    notes = ''

    def __init__(self, **kwargs):
        super(EditInput, self).__init__(**kwargs)
        print('NOTES', self.notes)
        #Clock.schedule_interval(self.set_text, 1)
        Clock.schedule_once(self.set_text, -1)

    def set_text(self, dt):
        print('SET TEXT', self.notes)
        self.ids.text_input.text = self.notes

class EditButton(BoxLayout):
    def __init__(self, **kwargs):
        super(EditButton, self).__init__(**kwargs)

    def change_tab(self):
        EditInput.notes = 'Lorem ipsum...'
        EditInput()

        mtp = App.get_running_app().root
        mtp.switch_to(mtp.ids.edit_input_tab)

class MainTabbedPanel(TabbedPanel):
    tab = ''
    def __init__(self, **kwargs):
        super(MainTabbedPanel, self).__init__(**kwargs)
        self.tabs_showing = True


class UnCloseableHeader(TabbedPanelHeader):
    pass

Factory.register('UnCloseableHeader', cls=UnCloseableHeader)


sm = Builder.load_string(theRoot)

class TabbedPanelApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TabbedPanelApp().run()

和:

def change_tab(self):
    EditInput.notes = 'Lorem ipsum...'
    EditInput()

在大约50%的情况下,女巫很难理解

1 个答案:

答案 0 :(得分:1)

您可以利用Property为您设置的kv绑定。将您的EditInput类更改为简单:

class EditInput(BoxLayout):
    notes = StringProperty('')

不需要任何方法。然后,在您的kv中,将EditInput规则更改为:

<EditInput>
    orientation: 'vertical'
    TextInput:
        id: text_input
        text: root.notes

并将change_tab的{​​{1}}方法更改为:

EditButton

请注意,更改def change_tab(self): mtp = App.get_running_app().root mtp.ids.edit_input.notes = 'Lorem ipsum...' mtp.switch_to(mtp.ids.edit_input_tab) 实例的notes属性将自动更改EditInput(由于TextInput设置了属性绑定)。

此外,kv中的行:

change_tab()

正在创建EditInput() 类的新实例,该实例未使用并将被垃圾回收。