如何从另一个 class/Screen 中的 TextInput 更改文本?
How to change text from TextInput that is in another class/Screen?
我在这些文件中重现了我这里的情况。
我有两个屏幕,我正在使用 ScreenManager,我想要的是,当我单击“生成证据”按钮时,我没有在控制台上打印,而是将我作为参数传递的内容打印在其他屏幕('Running',其中只有一个 TextInput)。但这并没有改变另一个屏幕上的文本,我想知道这是否可能以及为什么它不能这样工作。提前致谢。
mainApp.py
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
class Manager(ScreenManager):
def __init__(self):
super(ScreenManager, self).__init__()
class ScreenMain(Screen):
def __init__(self, **kwargs):
super(ScreenMain, self).__init__(**kwargs)
# This is called when you click the button
# Then the screen is changed
# Then call this other function that is down there
def change_text(self):
self.manager.current = 'Running'
print_whatever_is_happening()
# Second screen
class ScreenRunning(Screen):
def __init__(self, **kwargs):
super(ScreenRunning, self).__init__(**kwargs)
def write_message_on_text_input(self, message):
self.ids.data_log.text += message
class Example(MDApp):
def build(self):
sm = ScreenManager()
self.running_screen = ScreenRunning(name='Running')
self.main_screen = ScreenMain(name='ScreenMain')
sm.add_widget(self.running_screen)
sm.add_widget(self.main_screen)
sm.current = 'ScreenMain'
return sm
# This function is originally in a module, that's why it's outside the class
def print_whatever_is_happening():
# Instead of this...
print('Attaching 1')
print('Attaching 2')
print('Generating PDF file')
print('Attaching file')
# I would like to do this
ScreenRunning().write_message_on_text_input('Attaching 1')
ScreenRunning().write_message_on_text_input('Attaching 2')
ScreenRunning().write_message_on_text_input('Generating PDF file')
ScreenRunning().write_message_on_text_input('Attaching file')
Example().run()
example.kv
<Manager>:
id: screen_manager
screen_main: screen_main
screen_running: screen_running
ScreenMain:
id: screen_main
name: 'ScreenMain'
manager: screen_manager
ScreenRunning:
id: screen_running
name: 'Running'
manager: screen_manager
<ScreenMain>:
BoxLayout
id: Screen_One
orientation: 'vertical'
padding: '15dp'
canvas:
Color:
rgb: 0.878, 0.8, 0.984, 1
Rectangle:
size: self.size
pos: self.pos
MDRoundFlatIconButton:
id: evidence_button
text: 'Generate Evidence'
font_size: '17dp'
text_color: 1, 1, 1, 1
md_bg_color: 0.4, 0, 0.922, 1
on_press: root.manager.get_screen('ScreenMain').change_text()
<ScreenRunning>:
BoxLayout:
orientation: 'vertical'
padding: '15dp'
canvas:
Color:
rgb: 0.878, 0.8, 0.984, 1
Rectangle:
size: self.size
pos: self.pos
TextInput:
id: data_log
multiline: True
text: ''
您可以使用 MDApp.get_running_app()
获取对 App
的引用,然后使用 root
获取 App
的 root
(即ScreenManager
。然后您可以使用 get_screen()
方法获取对 ScreenRunning
实例的引用。使用这种方法,您的 print_whatever_is_happening()
方法可以是:
def print_whatever_is_happening():
# Instead of this...
print('Attaching 1')
print('Attaching 2')
print('Generating PDF file')
print('Attaching file')
# I would like to do this
screenRunning = MDApp.get_running_app().root.get_screen('Running')
screenRunning.write_message_on_text_input('Attaching 1')
screenRunning.write_message_on_text_input('Attaching 2')
screenRunning.write_message_on_text_input('Generating PDF file')
screenRunning.write_message_on_text_input('Attaching file')
我在这些文件中重现了我这里的情况。
我有两个屏幕,我正在使用 ScreenManager,我想要的是,当我单击“生成证据”按钮时,我没有在控制台上打印,而是将我作为参数传递的内容打印在其他屏幕('Running',其中只有一个 TextInput)。但这并没有改变另一个屏幕上的文本,我想知道这是否可能以及为什么它不能这样工作。提前致谢。
mainApp.py
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
class Manager(ScreenManager):
def __init__(self):
super(ScreenManager, self).__init__()
class ScreenMain(Screen):
def __init__(self, **kwargs):
super(ScreenMain, self).__init__(**kwargs)
# This is called when you click the button
# Then the screen is changed
# Then call this other function that is down there
def change_text(self):
self.manager.current = 'Running'
print_whatever_is_happening()
# Second screen
class ScreenRunning(Screen):
def __init__(self, **kwargs):
super(ScreenRunning, self).__init__(**kwargs)
def write_message_on_text_input(self, message):
self.ids.data_log.text += message
class Example(MDApp):
def build(self):
sm = ScreenManager()
self.running_screen = ScreenRunning(name='Running')
self.main_screen = ScreenMain(name='ScreenMain')
sm.add_widget(self.running_screen)
sm.add_widget(self.main_screen)
sm.current = 'ScreenMain'
return sm
# This function is originally in a module, that's why it's outside the class
def print_whatever_is_happening():
# Instead of this...
print('Attaching 1')
print('Attaching 2')
print('Generating PDF file')
print('Attaching file')
# I would like to do this
ScreenRunning().write_message_on_text_input('Attaching 1')
ScreenRunning().write_message_on_text_input('Attaching 2')
ScreenRunning().write_message_on_text_input('Generating PDF file')
ScreenRunning().write_message_on_text_input('Attaching file')
Example().run()
example.kv
<Manager>:
id: screen_manager
screen_main: screen_main
screen_running: screen_running
ScreenMain:
id: screen_main
name: 'ScreenMain'
manager: screen_manager
ScreenRunning:
id: screen_running
name: 'Running'
manager: screen_manager
<ScreenMain>:
BoxLayout
id: Screen_One
orientation: 'vertical'
padding: '15dp'
canvas:
Color:
rgb: 0.878, 0.8, 0.984, 1
Rectangle:
size: self.size
pos: self.pos
MDRoundFlatIconButton:
id: evidence_button
text: 'Generate Evidence'
font_size: '17dp'
text_color: 1, 1, 1, 1
md_bg_color: 0.4, 0, 0.922, 1
on_press: root.manager.get_screen('ScreenMain').change_text()
<ScreenRunning>:
BoxLayout:
orientation: 'vertical'
padding: '15dp'
canvas:
Color:
rgb: 0.878, 0.8, 0.984, 1
Rectangle:
size: self.size
pos: self.pos
TextInput:
id: data_log
multiline: True
text: ''
您可以使用 MDApp.get_running_app()
获取对 App
的引用,然后使用 root
获取 App
的 root
(即ScreenManager
。然后您可以使用 get_screen()
方法获取对 ScreenRunning
实例的引用。使用这种方法,您的 print_whatever_is_happening()
方法可以是:
def print_whatever_is_happening():
# Instead of this...
print('Attaching 1')
print('Attaching 2')
print('Generating PDF file')
print('Attaching file')
# I would like to do this
screenRunning = MDApp.get_running_app().root.get_screen('Running')
screenRunning.write_message_on_text_input('Attaching 1')
screenRunning.write_message_on_text_input('Attaching 2')
screenRunning.write_message_on_text_input('Generating PDF file')
screenRunning.write_message_on_text_input('Attaching file')