![]() |
|---|
| © dotmodus |
由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址
Quick view
![]() |
|---|
| (c) Karobben |
This work originated from @一个哲学家[1]
Here is the main.py
from kivy.app import Appfrom kivy.lang import Builderfrom kivy.uix.screenmanager import ScreenManager, Screenfrom kivy.uix.button import Buttonfrom kivy.uix.widget import Widgetfrom kivy.properties import ObjectPropertyfrom kivy.core.text import LabelBaseclass NewGameScreen(Screen):class Gao(Widget):label = ObjectProperty(None)def btn(self,label):label.text='AFTER PROCESSING'+label.textclass OptionScreen(Screen):passclass TestApp(App):def build(self):sm = ScreenManager()sm.add_widget(NewGameScreen())sm.add_widget(OptionScreen())return smif __name__ == '__main__':TestApp().run()
here is the test.kv
<NewGameScreen>:name: 'newgame'label:label_idBoxLayout:orientation: 'vertical'TextInput:id:label_idtext:'INPUT'on_text:root.manager.get_screen('options').label.text = str(self.text)Button:text: 'SUBMIT'on_press:root.Gao.btn(self,label_id)root.manager.transition.direction = 'left'root.manager.current = 'options'root.manager.current = 'options' if label_id.text == '123' else "newgame" #输入为 123 时才跳转<OptionScreen>:label: label_idname: 'options'orientation: 'vertical'BoxLayout:Button:text: 'BACK'on_press:root.manager.transition.direction = 'right'root.manager.current = 'newgame'Label:id: label_idtext: '1'
Similar Resolution
This script was also communicate the different classes by using manager.get_screen @Nykakin [2]
from kivy.app import Appfrom kivy.lang import Builderfrom kivy.uix.screenmanager import ScreenManager, Screenfrom kivy.properties import StringPropertyBuilder.load_string('''<MainScreen>:BoxLayout:orientation: "vertical"Button:text: 'Goto strategy'on_press: root.manager.current = 'strategy'Button:text: 'Set text'on_press: root.SetText()<StrategyScreen>:BoxLayout:orientation: "vertical"Label:text: root.labelTextButton:text: 'Back to menu'on_press: root.manager.current = 'main'''')class MainScreen(Screen):def SetText(self):text = 'Total=' + str(17*21)self.manager.get_screen('strategy').labelText = textclass StrategyScreen(Screen):labelText = StringProperty('My label')class TestApp(App):def build(self):# Create the screen managerscreenManager = ScreenManager()screenManager.add_widget(MainScreen(name='main'))screenManager.add_widget(StrategyScreen(name='strategy'))return screenManagerif __name__ == '__main__':TestApp().run()
Another Example:
main.py
from kivy.app import Appfrom kivy.uix.anchorlayout import AnchorLayoutfrom kivy.uix.boxlayout import BoxLayoutfrom kivy.uix.popup import Popupclass ConfirmPopup(BoxLayout):def __init__(self, **kwargs):self.register_event_type('on_answer')super(ConfirmPopup, self).__init__(**kwargs)self.total_images = 0def on_answer(self, filename, JKMain):self.total_images = 8print("JKMain=", JKMain)JKMain.change_text(self.total_images)class JKMain(AnchorLayout):def __init__(self, **kwargs):super(JKMain, self).__init__(**kwargs)def change_text(self, layers):self.the_time.text = "Total Layers : " + str(layers)print("Total Layers = " + str(layers))def popup_func(self):content = ConfirmPopup()content.bind(on_answer=self._on_answer)self.popup = Popup(title="Select .zip file",content=content,size_hint=(None, None),size=(500, 500),auto_dismiss=False)self.popup.open()def _on_answer(self, instance, answer, obj):self.popup.dismiss()class Main(App):def build(self):return JKMain()if __name__ == "__main__":Main().run()
main.ky
#: kivy 1.10.0<JKmain>:the_time: _id_lbl_timeAnchorLayout:anchor_x: 'left'anchor_y: 'top'BoxLayout:orientation: 'vertical'id: _tool_boxsize_hint: None,0.75width: 300Label:id: _id_lbl_timetext: "Total Layers : "AnchorLayout:anchor_x: 'right'anchor_y: 'top'GridLayout:rows:2BoxLayout:orientation: 'horizontal'Button:on_release: app.root.current = "main"text: "SELECT"size_hint: 1,0.2background_color: (1.0, 1.0, 0.0, 1.0)on_release: root.popup_func()Button:text: "START"size_hint: 1,0.2background_color: (1.0, 0.0, 1.0, 1.0)on_release: root.change_text(100)Button:text: "EXIT"size_hint: 1,0.2background_color: (1.0, 0.0, 1.0, 1.0)on_release: root.exit_app(self)<ConfirmPopup>:BoxLayout:orientation: 'vertical'FileChooserIconView:id: filechooserfilters: ['*.zip']GridLayout:cols: 2size_hint: 1,0.2Button:text: 'OK'on_release: root.dispatch('on_answer', filechooser.selection, app.root)size_hint: 1,0.2Button:text: 'Cancel'on_release: root.dispatch('on_answer', 'Cancel')size_hint: 1,0.2
Enjoy~
由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址
GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗


