Kivy: 不同屏幕之間的參數傳遞 - 图1
© dotmodus

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Quick view

Kivy: 不同屏幕之間的參數傳遞 - 图2
(c) Karobben

This work originated from @一个哲学家[1]

Here is the main.py

  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.uix.screenmanager import ScreenManager, Screen
  4. from kivy.uix.button import Button
  5. from kivy.uix.widget import Widget
  6. from kivy.properties import ObjectProperty
  7. from kivy.core.text import LabelBase
  8. class NewGameScreen(Screen):
  9. class Gao(Widget):
  10. label = ObjectProperty(None)
  11. def btn(self,label):
  12. label.text='AFTER PROCESSING'+label.text
  13. class OptionScreen(Screen):
  14. pass
  15. class TestApp(App):
  16. def build(self):
  17. sm = ScreenManager()
  18. sm.add_widget(NewGameScreen())
  19. sm.add_widget(OptionScreen())
  20. return sm
  21. if __name__ == '__main__':
  22. TestApp().run()

here is the test.kv

  1. <NewGameScreen>:
  2. name: 'newgame'
  3. label:label_id
  4. BoxLayout:
  5. orientation: 'vertical'
  6. TextInput:
  7. id:label_id
  8. text:'INPUT'
  9. on_text:
  10. root.manager.get_screen('options').label.text = str(self.text)
  11. Button:
  12. text: 'SUBMIT'
  13. on_press:
  14. root.Gao.btn(self,label_id)
  15. root.manager.transition.direction = 'left'
  16. root.manager.current = 'options'
  17. root.manager.current = 'options' if label_id.text == '123' else "newgame" #输入为 123 时才跳转
  18. <OptionScreen>:
  19. label: label_id
  20. name: 'options'
  21. orientation: 'vertical'
  22. BoxLayout:
  23. Button:
  24. text: 'BACK'
  25. on_press:
  26. root.manager.transition.direction = 'right'
  27. root.manager.current = 'newgame'
  28. Label:
  29. id: label_id
  30. text: '1'

Similar Resolution

This script was also communicate the different classes by using manager.get_screen @Nykakin [2]

  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.uix.screenmanager import ScreenManager, Screen
  4. from kivy.properties import StringProperty
  5. Builder.load_string('''
  6. <MainScreen>:
  7. BoxLayout:
  8. orientation: "vertical"
  9. Button:
  10. text: 'Goto strategy'
  11. on_press: root.manager.current = 'strategy'
  12. Button:
  13. text: 'Set text'
  14. on_press: root.SetText()
  15. <StrategyScreen>:
  16. BoxLayout:
  17. orientation: "vertical"
  18. Label:
  19. text: root.labelText
  20. Button:
  21. text: 'Back to menu'
  22. on_press: root.manager.current = 'main'
  23. ''')
  24. class MainScreen(Screen):
  25. def SetText(self):
  26. text = 'Total=' + str(17*21)
  27. self.manager.get_screen('strategy').labelText = text
  28. class StrategyScreen(Screen):
  29. labelText = StringProperty('My label')
  30. class TestApp(App):
  31. def build(self):
  32. # Create the screen manager
  33. screenManager = ScreenManager()
  34. screenManager.add_widget(MainScreen(name='main'))
  35. screenManager.add_widget(StrategyScreen(name='strategy'))
  36. return screenManager
  37. if __name__ == '__main__':
  38. TestApp().run()

Another Example:

@Taher Kawantwala [3]

main.py

  1. from kivy.app import App
  2. from kivy.uix.anchorlayout import AnchorLayout
  3. from kivy.uix.boxlayout import BoxLayout
  4. from kivy.uix.popup import Popup
  5. class ConfirmPopup(BoxLayout):
  6. def __init__(self, **kwargs):
  7. self.register_event_type('on_answer')
  8. super(ConfirmPopup, self).__init__(**kwargs)
  9. self.total_images = 0
  10. def on_answer(self, filename, JKMain):
  11. self.total_images = 8
  12. print("JKMain=", JKMain)
  13. JKMain.change_text(self.total_images)
  14. class JKMain(AnchorLayout):
  15. def __init__(self, **kwargs):
  16. super(JKMain, self).__init__(**kwargs)
  17. def change_text(self, layers):
  18. self.the_time.text = "Total Layers : " + str(layers)
  19. print("Total Layers = " + str(layers))
  20. def popup_func(self):
  21. content = ConfirmPopup()
  22. content.bind(on_answer=self._on_answer)
  23. self.popup = Popup(title="Select .zip file",
  24. content=content,
  25. size_hint=(None, None),
  26. size=(500, 500),
  27. auto_dismiss=False)
  28. self.popup.open()
  29. def _on_answer(self, instance, answer, obj):
  30. self.popup.dismiss()
  31. class Main(App):
  32. def build(self):
  33. return JKMain()
  34. if __name__ == "__main__":
  35. Main().run()

main.ky

  1. #: kivy 1.10.0
  2. <JKmain>:
  3. the_time: _id_lbl_time
  4. AnchorLayout:
  5. anchor_x: 'left'
  6. anchor_y: 'top'
  7. BoxLayout:
  8. orientation: 'vertical'
  9. id: _tool_box
  10. size_hint: None,0.75
  11. width: 300
  12. Label:
  13. id: _id_lbl_time
  14. text: "Total Layers : "
  15. AnchorLayout:
  16. anchor_x: 'right'
  17. anchor_y: 'top'
  18. GridLayout:
  19. rows:2
  20. BoxLayout:
  21. orientation: 'horizontal'
  22. Button:
  23. on_release: app.root.current = "main"
  24. text: "SELECT"
  25. size_hint: 1,0.2
  26. background_color: (1.0, 1.0, 0.0, 1.0)
  27. on_release: root.popup_func()
  28. Button:
  29. text: "START"
  30. size_hint: 1,0.2
  31. background_color: (1.0, 0.0, 1.0, 1.0)
  32. on_release: root.change_text(100)
  33. Button:
  34. text: "EXIT"
  35. size_hint: 1,0.2
  36. background_color: (1.0, 0.0, 1.0, 1.0)
  37. on_release: root.exit_app(self)
  38. <ConfirmPopup>:
  39. BoxLayout:
  40. orientation: 'vertical'
  41. FileChooserIconView:
  42. id: filechooser
  43. filters: ['*.zip']
  44. GridLayout:
  45. cols: 2
  46. size_hint: 1,0.2
  47. Button:
  48. text: 'OK'
  49. on_release: root.dispatch('on_answer', filechooser.selection, app.root)
  50. size_hint: 1,0.2
  51. Button:
  52. text: 'Cancel'
  53. on_release: root.dispatch('on_answer', 'Cancel')
  54. size_hint: 1,0.2

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗


  1. 一个哲学家; 2021; 今日头条; kivy教程:实现屏幕切换、数据传递和函数绑定 ↩︎

  2. Psionman; 2015; How to update label text on second kivy screen; StackOverflow ↩︎

  3. Taher Kawantwala; 2018; Kivy Change Label widget Text from another class; StackOverflow ↩︎