<small id='b5YqR'></small><noframes id='b5YqR'>

        <bdo id='b5YqR'></bdo><ul id='b5YqR'></ul>
      <i id='b5YqR'><tr id='b5YqR'><dt id='b5YqR'><q id='b5YqR'><span id='b5YqR'><b id='b5YqR'><form id='b5YqR'><ins id='b5YqR'></ins><ul id='b5YqR'></ul><sub id='b5YqR'></sub></form><legend id='b5YqR'></legend><bdo id='b5YqR'><pre id='b5YqR'><center id='b5YqR'></center></pre></bdo></b><th id='b5YqR'></th></span></q></dt></tr></i><div id='b5YqR'><tfoot id='b5YqR'></tfoot><dl id='b5YqR'><fieldset id='b5YqR'></fieldset></dl></div>

      <legend id='b5YqR'><style id='b5YqR'><dir id='b5YqR'><q id='b5YqR'></q></dir></style></legend>
    1. <tfoot id='b5YqR'></tfoot>

      如何为每个屏幕添加一个自己的 .py 和 .kv 文件?

      时间:2023-06-06

        <tbody id='rFI7M'></tbody>

      • <legend id='rFI7M'><style id='rFI7M'><dir id='rFI7M'><q id='rFI7M'></q></dir></style></legend>
        <tfoot id='rFI7M'></tfoot>
        <i id='rFI7M'><tr id='rFI7M'><dt id='rFI7M'><q id='rFI7M'><span id='rFI7M'><b id='rFI7M'><form id='rFI7M'><ins id='rFI7M'></ins><ul id='rFI7M'></ul><sub id='rFI7M'></sub></form><legend id='rFI7M'></legend><bdo id='rFI7M'><pre id='rFI7M'><center id='rFI7M'></center></pre></bdo></b><th id='rFI7M'></th></span></q></dt></tr></i><div id='rFI7M'><tfoot id='rFI7M'></tfoot><dl id='rFI7M'><fieldset id='rFI7M'></fieldset></dl></div>

        <small id='rFI7M'></small><noframes id='rFI7M'>

              • <bdo id='rFI7M'></bdo><ul id='rFI7M'></ul>
              • 本文介绍了如何为每个屏幕添加一个自己的 .py 和 .kv 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我希望每个屏幕都有一个单独的 .py 和 .kv 文件.在 main.py/main.kv 中的 ScreenManager 上应该选择屏幕.设计应从文件 screen_X.kv 加载,类等应从文件 screen_X.py 加载.

                I want to have a seperate .py and .kv file for each sreen. Over the ScreenManager in main.py/main.kv the screen should be selected. The design should be loaded from the file screen_X.kv and the classes etc. should be loaded from the file screen_X.py.

                屏幕:

                • 屏幕 1
                • 屏幕 2
                • 屏幕 3

                文件:

                • (main.py)
                • (showcase.kv)
                • screen_1.py
                • screen_1.kv
                • screen_2.py
                • screen_2.kv
                • screen_3.py
                • screen_3.kv

                这使得程序可以很容易地扩展.我怎样才能做到这一点?

                This makes the program can be easily extended. How can I achieve this?

                使用此代码,我已经分离了 .kv 文件.但我还需要单独的 .py 文件.

                With this code I have seperated .kv files. But I need also separated .py files.

                main.py

                from time import time
                from kivy.app import App
                from os.path import dirname, join
                from kivy.lang import Builder
                from kivy.properties import NumericProperty, BooleanProperty, ListProperty
                from kivy.clock import Clock
                from kivy.uix.screenmanager import Screen
                
                
                class ShowcaseScreen(Screen):
                    fullscreen = BooleanProperty(False)
                
                    def add_widget(self, *args):
                        if 'content' in self.ids:
                            return self.ids.content.add_widget(*args)
                        return super(ShowcaseScreen, self).add_widget(*args)
                
                
                class ShowcaseApp(App):
                    time = NumericProperty(0)
                    screen_names = ListProperty([])
                    bool_menu = BooleanProperty(False)
                
                    def build(self):
                        self.title = 'hello world'
                        Clock.schedule_interval(self._update_clock, 1 / 60.)
                        self.available_screens = [
                            'Buttons', 'CheckBoxes', 'ProgressBar', 'Switches', 'ToggleButton',
                        ]
                        self.screen_names = self.available_screens
                        curdir = dirname(__file__)
                        self.available_screens = [join(curdir, 'data', 'screens', '{}.kv'.format(fn)) for fn in self.available_screens]
                
                        self.menu_screen = join(curdir, 'data', 'screens', '{}.kv'.format('Menu'))
                
                        self.go_menu()
                
                    def go_screen(self, idx):
                        screen = Builder.load_file(self.available_screens[idx])
                        self.root.ids.sm.switch_to(screen, direction='left')
                
                    def go_menu(self):
                        if not self.bool_menu:
                            screen = Builder.load_file(self.menu_screen)
                            self.root.ids.sm.switch_to(screen, direction='right')
                
                    def _update_clock(self, dt):
                        self.time = time()
                
                if __name__ == '__main__':
                    ShowcaseApp().run()
                

                showcase.kv

                showcase.kv

                #:kivy 1.8.0
                #:import KivyLexer kivy.extras.highlight.KivyLexer
                #:import Factory kivy.factory.Factory
                
                <ActionSpinnerOptions@SpinnerOption>
                    background_color: .4, .4, .4, 1
                
                <ActionSpinner@Spinner+ActionItem>
                    canvas.before:
                        Color:
                            rgba: 0.128, 0.128, 0.128, 1
                        Rectangle:
                            size: self.size
                            pos: self.pos
                    border: 27, 20, 12, 12
                    background_normal: 'atlas://data/images/defaulttheme/action_group'
                    option_cls: Factory.ActionSpinnerOptions
                
                <ActionDropdown>:
                    on_size: self.width = '220dp'
                
                <ShowcaseScreen>:
                    ScrollView:
                        do_scroll_x: False
                        do_scroll_y: False if root.fullscreen else (content.height > root.height - dp(16))
                        AnchorLayout:
                            size_hint_y: None
                            height: root.height if root.fullscreen else max(root.height, content.height)
                            GridLayout:
                                id: content
                                cols: 1
                                spacing: '8dp'
                                padding: '8dp'
                                size_hint: (1, 1) if root.fullscreen else (.8, None)
                                height: self.height if root.fullscreen else self.minimum_height
                
                
                BoxLayout:
                    orientation: 'vertical'
                
                    canvas.before:
                        Color:
                            rgb: .6, .6, .6
                        Rectangle:
                            size: self.size
                            source: 'data/background.png'
                
                    ActionBar:
                
                        ActionView:
                            id: av
                            ActionPrevious:
                                with_previous: (False if app.bool_menu == True else True) if not app.bool_menu else False
                                title: 'Menu'
                                on_release: app.go_menu()
                
                            ActionSpinner:
                                id: spnr
                                important: True
                                text: 'Select Program'
                                values: app.screen_names
                                on_text:
                                    if (spnr.text != 'Select Program') and (sm.current != args[1]):
                                    idx = app.screen_names.index(args[1]);
                                    app.go_screen(idx)
                
                    ScreenManager:
                        id: sm
                        on_current_screen:
                            screen_name = args[1].name
                
                            spnr.text = 'Select Program' if screen_name == 'Menu' else screen_name
                
                            if screen_name == 'Menu': app.bool_menu = True
                            else: app.bool_menu = False
                

                推荐答案

                首先,你没有提到你是否知道如何按通常的方式设置不同的屏幕,我不确定我在上面的代码.如果您不这样做,这里有一个相当简单的教程来创建一个简单的多屏幕设置.

                First of all, you haven't mentioned whether you know how to set up different screens the usual way, and I'm not sure that I see it in the code above. In case you don't, there is a fairly simple tutorial here on creating a simple multi-screen setup.

                每个屏幕都是继承自 Screen 的类,只需在单独的 .py 文件中定义这些屏幕类,然后将它们导入到您的 main.py 文件中,这并不是一件难事.我以前做过.例如,我有我的 main.py(你需要它),我在一个名为game_screen.py"的 py 文件中定义了我所有的各种屏幕,并从那里简单地导入.

                Each screen being a class that inherits from Screen, it isn't a difficult thing to just define those screen classes in seperate .py files and then import them into your main.py file. I have done this before. For instance, I had my main.py(which you need), and I had all my various screens defined in a py file called 'game_screen.py', and simply imported from there.

                我自己并没有看到过多使用多个 kv 文件,但我知道您可以使用与下面类似的代码将 kv 规则添加到您的主(默认)kv 文件规则中.尽管我相信这些规则必须在它们生效的小部件之前加载才能正常工作.

                I haven't seen much use of multiple kv files myself, but I know you can add kv rules to your main(default) kv files rules using similar code to the below. Though these rules, I believe, must be loaded before the widgets they will effect in order to work correctly.

                from kivy.lang import Builder
                Builder.load_file('screen1.kv')
                Builder.load_file('screen2.kv')
                Builder.load_file('screen3.kv')
                

                因此,通过将您的屏幕类导入您的 main.py 并使用上述添加 kv 文件的方法,您可能会实现您的要求......然后决定这是否是一个好方法.

                So by importing your screen classes to your main.py and using the above method of adding kv files, you could probably achieve what you are asking for.. and then decide if that's a good way to go.

                这篇关于如何为每个屏幕添加一个自己的 .py 和 .kv 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Kivy:BoxLayout 与 GridLayout 下一篇:在 Kivy 应用程序中未播放 Gif 动画

                相关文章

                • <bdo id='oOPJ2'></bdo><ul id='oOPJ2'></ul>
                <legend id='oOPJ2'><style id='oOPJ2'><dir id='oOPJ2'><q id='oOPJ2'></q></dir></style></legend>

              • <tfoot id='oOPJ2'></tfoot>

                <small id='oOPJ2'></small><noframes id='oOPJ2'>

              • <i id='oOPJ2'><tr id='oOPJ2'><dt id='oOPJ2'><q id='oOPJ2'><span id='oOPJ2'><b id='oOPJ2'><form id='oOPJ2'><ins id='oOPJ2'></ins><ul id='oOPJ2'></ul><sub id='oOPJ2'></sub></form><legend id='oOPJ2'></legend><bdo id='oOPJ2'><pre id='oOPJ2'><center id='oOPJ2'></center></pre></bdo></b><th id='oOPJ2'></th></span></q></dt></tr></i><div id='oOPJ2'><tfoot id='oOPJ2'></tfoot><dl id='oOPJ2'><fieldset id='oOPJ2'></fieldset></dl></div>