【问题标题】:python kivy screen confogpython kivy屏幕配置
【发布时间】:2020-09-02 17:47:14
【问题描述】:
main.py file  
from kivymd.app import MDApp
from kivy.lang import Builder
class Hero(MDApp):
    def build(self):
        return Builder.load_file("stackoverflow.kv")
Hero().run()

以上是main.py文件 请大家帮帮我

stackoverflow.kv 文件

ScreenManager:
    id: screenmanager
    Screen:
        id: accountscreen
        name: "screen1"
        BoxLayout:
            oreintation: "vertical"
            MDGridLayout:
                cols:1## Heading ##
               MDLabel:
                    text: "[color=00FF00]Sign In[/color]"
                    bold:True
                    markup:True
                    halign: "center"
                    valign: "middle"
                MDTextField:
                    id: username
                    padding: "30dp"
                    spacing: "30dp"
                    hint_text:"User Name or Email"
                    pos_hint: {"center_x":.5,"center_y":.5}
                MDTextField:
                    id:password
                    padding: "20dp"
                    spacing: "20dp"
                    hint_text:"Password"
                MDRectangleFlatButton:
                    text:"Sign In"
                    padding: "16dp"
                    spacing: "16dp"
                    halign: "center"
                    pos_hint: {"center_x":.9,"center_y":.5}
                    on_press: app.root.current="screen2"

                BoxLayout:
                    oreintation: "vertical"
    Screen:
        id: welcome_screen
        name: "screen2"
        MDLabel:
            text: "welcome"
            valign: "middle"
            halign: "center"

每当我打开应用程序时,我都会看到登录屏幕,如何使用配置 ini 文件保存用户信息并清除屏幕,请帮助我????

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    这是你代码的一个版本,它使用Config 来做你想做的事:

    import os
    from kivymd.app import MDApp
    from kivy.lang import Builder
    
    class Hero(MDApp):
        def build(self):
            sm = Builder.load_file("stackoverflow.kv")
            self.load_my_config()
            sm.current = self.initial_screen
            return sm
    
        def build_config(self, config):
            # Make sure that the config has at least default entries that we need
            config.setdefaults('app', {
                'startup_screen': 'screen1',
            })
    
        def get_application_config(self):
            # This defines the path and name where the ini file is located
            return str(os.path.join(os.path.expanduser('~'), 'Hero.ini'))
    
        def on_stop(self):
            # save the config when the app exits
            self.save_config()
    
        def save_config(self):
            # this writes the data we want to save to the config
    
            # set the data in the config
            self.config.set('app', 'startup_screen', 'screen2')
    
            # write the config file
            self.config.write()
    
        def load_my_config(self):
            # extract our saved data from the config (it has already been read)
            self.initial_screen = self.config.get('app', 'startup_screen', fallback='screen1')
    
    Hero().run()
    

    kv 文件中,我会添加一个调用以在按下Sign In 按钮时保存配置:

                MDRectangleFlatButton:
                    text:"Sign In"
                    padding: "16dp"
                    spacing: "16dp"
                    halign: "center"
                    pos_hint: {"center_x":.9,"center_y":.5}
                    on_press:
                        app.root.current="screen2"
                        app.save_config()
    

    【讨论】:

      猜你喜欢
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多