![](/img/trans.png)
[英]In Kivy How do i get a variable from another screen displayed in another in kv language
[英]How do I change the text of a button on another screen in kivy (without kv lang)
我想更改另一个屏幕上的按钮文本,但我不使用 kv 语言,我该怎么做? 这是一个代码示例
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
list=['Yes','No']
class ScreenManagement(ScreenManager):
def __init__(self, **kwargs):
super(ScreenManagement, self).__init__(**kwargs)
class Iniciar(Screen):
def __init__(self, **kwargs):
super(Iniciar, self).__init__(**kwargs)
self.btn = Button(text='INICIAR', size_hint=(.2, .1), pos_hint={'center_x': .5, 'y': .05}, background_color=(0, 0, 1.60, 1),font_size=24, color=(.8, .9, 0, 1))
self.btn.bind(on_press=self.press_button)
self.add_widget(self.btn)
def press_button(self, *args):
list[0] = 'Apple'
list[1] = 'Banana'
self.manager.current = 'categorical'
class Categorical(Screen):
def __init__(self, **kwargs):
super(Categorical, self).__init__(**kwargs)
self.button_1 = Button(text=list[0], size_hint=(.2, .1), pos_hint={'center_x': .35, 'y': .05})
self.button_2 = Button(text=list[1], size_hint=(.2, .1), pos_hint={'center_x': .64, 'y': .05})
self.add_widget(self.button_1)
self.add_widget(self.button_2)
class Application(App):
def build(self):
sm = ScreenManagement(transition=FadeTransition())
sm.add_widget(Iniciar(name='iniciar'))
sm.add_widget(Categorical(name='categorical'))
return sm
if __name__ == "__main__":
Application().run()
在这种情况下,我更改了包含按钮文本值的列表
您不需要为此使用列表。 一种简单的方法是将按钮小部件分配给变量并使用button.text
。 就像在您的代码中一样,您已将 Button 小部件分配给名为self.button_1
和self.button_2
的变量。 因此,要更改 button_1 的文本,只需对 button_2 执行self.button_1.text = 'new'
, self.button_2.text='new2'
self.manager.get_screen('ScreenWithButton').ids['buttonid'].text = ""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.