因为 print('hello') 在文本输入中按回车键后正在工作,但为什么这两个不起作用?

since print('hello') is working after pressing enter in Text Input but Why don't these two work?

因为 print('hello')Text Input 中按回车键后可以正常工作,但是为什么这两个不起作用?

      self.grid2.add_widget(Button())
      self.grid2.add_widget(TextInput())

代码:

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.button import Button

 

Builder.load_string("""

<Button>
    size_hint: (None, None)
    height: 33
    width: 100
    text:'1'
    background_normal: ''
    background_down:'1, .3, .4, .85'
    background_color: 1, .3, .4, .85



<TextInput>
    
    selection_color:1, .3, .4, .4
    on_text_validate:app.enter()
    cursor_color: 255/255, 223/255, 5/255, 1
    multiline:False
    height: 33
    width:800
    size_hint: (None, None)
    background_color: 0,0,0,0
    foreground_color: 255/255, 143/255, 5/255, 0.8 


    
<Grid>
    
    t1:t1
    b1:b1
    grid:grid
    grid2:grid2
    
    GridLayout:
        cols:1
        id:grid
        size: root.width - 400,root.height - 400
        pos:100,100
        
        GridLayout:
            id:grid2
            cols:2
             
        
            Button:
                id:b1
 
                  

            TextInput:
                id:t1
                 

""")

 

class Grid(Widget):
    t1=ObjectProperty(None)
    b1=ObjectProperty(None)
    grid=ObjectProperty(None)
    grid2=ObjectProperty(None)
    def enter(self):
              print('hello')
              self.grid2.add_widget(Button())
              self.grid2.add_widget(TextInput())
               

gr=Grid() 
class foo(App):

    def build(self):
        Window.clearcolor='#1618388'
       
        return Grid()
    def enter(self):
        gr.enter()
     





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

如果我为 Grid() 返回 gr 而不是它,它对我有用

class foo(App):
    def build(self):
        Window.clearcolor = '#1618388'
        return gr

    def enter(self):
        gr.enter()