简体   繁体   English

将 ScreenManager 与每个屏幕的单独 py 和 kv 文件一起使用

[英]Using ScreenManager with separate py and kv files for each screen

I'm making a calculator app and I want to be able to use separate kv and py files for each screen, I haven't found any documentation or tutorial about this and the only answer I have found doesn't give a code example so I'm not sure how to implement it.我正在制作一个计算器应用程序,我希望能够为每个屏幕使用单独的 kv 和 py 文件,我没有找到任何关于此的文档或教程,我找到的唯一答案没有给出代码示例所以我不确定如何实现它。 Also the buttons on calc.kv send a callback to calc.py by referencing it in root so that could probably cause a problem because the root is screenmanager此外,calc.kv 上的按钮通过在 root 中引用它来向 calc.py 发送回调,这样可能会导致问题,因为 root 是 screenmanager

If you try to open the calculator from the toolbar it will return this error message:如果您尝试从工具栏中打开计算器,它将返回此错误消息:

 screen = self.get_screen(value)
   File "C:\Python38\lib\site-packages\kivy\uix\screenmanager.py", line 1071, in get_screen
     raise ScreenManagerException('No Screen with name "%s".' % name)
 kivy.uix.screenmanager.ScreenManagerException: No Screen with name "calc".

here is the code:这是代码:

calc.py计算.py

from logging import root
from kivymd.app import MDApp
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.properties import StringProperty
from kivy.lang import Builder


prev = ""
expression = "" 
prox = "" 
class calc(Widget):
    pass
    out = StringProperty('')
    Window.size = (420, 200)
    Window.minimum_width, Window.minimum_height = Window.size
    
    
    
    def my_callback(self, inpt):
        global prev
        global expression
        global prox
        
            
        if inpt == "=":
            prox = str(eval(expression))
            self.out = prox
            expression = ""
            prev = prox
            
            
        elif inpt == "ac":
            expression = ""
            prev = ""
            prox = ""
            self.out = expression
        
        
            
        else:
            expression = prev + inpt
            prev = expression
            self.out = expression
        
         
        
                    
class calcApp(MDApp):  
    def build(self):
        def press(text):
            print("called: ", text)
    
        return calc()


if __name__ == '__main__':
    calcApp().run()

calc.kv计算.kv

#:kivy 1.0.9

   

<calc>:
    BoxLayout:
        orientation: 'vertical'
        
        cols: 2
        size: root.width, root.height

        MDLabel:
            id: 'output'
            text: str(root.out)
            size_hint: 1, 0.2
            halign: 'center'

        GridLayout:
                       
            cols: 5
            MDFlatButton:
                
                text: '1'
                on_press: root.my_callback(self.text)
                
            MDFlatButton:
                
                text: '2' 
                on_press: root.my_callback(self.text)
            
            MDFlatButton:
                
                text: '3'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '-'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                text: 'ac'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '4'  
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '5' 
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '6'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '+' 
                on_press: root.my_callback(self.text)

            MDFlatButton:
                text: '('
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '7' 
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '8' 
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '9' 
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '*'
                on_press: root.my_callback(self.text)
            
            MDFlatButton:
                text: ')'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '0' 
                on_press: root.my_callback(self.text)
            MDFlatButton:
                
                text: '.'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                text: '/'
                on_press: root.my_callback(self.text)

            
            MDFlatButton:
                
                text: '**'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '='
                on_press: root.my_callback(self.text)            
            

main.kv主文件

#:kivy 1.0.9

#: include calc.kv

NavigationLayout:
    id: nav_layout

    ScreenManager:

        Screen:

            BoxLayout:
                orientation: 'vertical'

                MDToolbar:
                    title: app.title
                    elevation: 10
                    left_action_items: [['menu', lambda x: nav_drawer.set_state()]]

                ScreenManager:
                    id: screen_manager
                    calculator:
                        
                    

    MDNavigationDrawer:
        id: nav_drawer

        BoxLayout:
            orientation: 'vertical'
            padding: '8dp'
            spacing: '8dp'

            Image:
                size_hint: None, None
                size: '280dp', "200dp"
                source: "logo.png"
            
            ScrollView:
            
                MDList:

                    OneLineIconListItem:
                        text: "calculadora"
                        on_release:
                            screen_manager.current = 'calc'
                            nav_drawer.set_state()
                        IconLeftWidget:
                            icon: 'view-dashboard'

main.py主文件

import calc
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

class ScreenManager(ScreenManager):
    pass

class MyApp(MDApp):
    def build(self):
        self.title = "calculadora"
        self.theme_cls.primary_palette = "Green"
        return Builder.load_file("main.kv")


MyApp().run()

thanks in advance.提前致谢。

You need to give a name to the <calc> in the calc.kv file and instead of inheriting from the Widget class, inherit from the Screen class in the calc.py file.您需要为 calc.kv 文件中的<calc>命名,而不是从Widget class 继承,而是从 calc.py 文件中的Screen class 继承。 After that you need to load the calc.kv file in main.py using Builder.load_file("calc.kv") and then add the calc() Screen in your.kv with a function like on_release之后,您需要使用Builder.load_file("calc.kv")在 your.kv 中添加calc() Screen ,如on_release

calc.py (just change inheritance from Widget to Screen over here) calc.py (只需在此处将 inheritance 从Widget更改为Screen

from logging import root
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
from kivy.core.window import Window
from kivy.properties import StringProperty
from kivy.lang import Builder


prev = ""
expression = "" 
prox = "" 
class calc(Screen):
    out = StringProperty('')
    Window.size = (420, 200)
    Window.minimum_width, Window.minimum_height = Window.size
    
    
    
    def my_callback(self, inpt):
        global prev
        global expression
        global prox
        
            
        if inpt == "=":
            prox = str(eval(expression))
            self.out = prox
            expression = ""
            prev = prox
            
            
        elif inpt == "ac":
            expression = ""
            prev = ""
            prox = ""
            self.out = expression
        
        
            
        else:
            expression = prev + inpt
            prev = expression
            self.out = expression
        
         
        
                    
class calcApp(MDApp):  
    def build(self):
        def press(text):
            print("called: ", text)
    
        return calc()


if __name__ == '__main__':
    calcApp().run()

calc.kv (just give a name to the class for reference) calc.kv (只需给 class 起一个名称以供参考)

#:kivy 1.0.9

   

<calc>:
    name: 'calc'
    BoxLayout:
        orientation: 'vertical'
        
        cols: 2
        size: root.width, root.height

        MDLabel:
            id: 'output'
            text: str(root.out)
            size_hint: 1, 0.2
            halign: 'center'

        GridLayout:
                       
            cols: 5
            MDFlatButton:
                
                text: '1'
                on_press: root.my_callback(self.text)
                
            MDFlatButton:
                
                text: '2' 
                on_press: root.my_callback(self.text)
            
            MDFlatButton:
                
                text: '3'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '-'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                text: 'ac'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '4'  
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '5' 
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '6'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '+' 
                on_press: root.my_callback(self.text)

            MDFlatButton:
                text: '('
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '7' 
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '8' 
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '9' 
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '*'
                on_press: root.my_callback(self.text)
            
            MDFlatButton:
                text: ')'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '0' 
                on_press: root.my_callback(self.text)
            MDFlatButton:
                
                text: '.'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                text: '/'
                on_press: root.my_callback(self.text)

            
            MDFlatButton:
                
                text: '**'
                on_press: root.my_callback(self.text)

            MDFlatButton:
                
                text: '='
                on_press: root.my_callback(self.text)

main.py (just load calc.kv in the beginning) main.py (只需在开始时加载 calc.kv)

import calc
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

Builder.load_file("calc.kv")

class MyApp(MDApp):
    def build(self):
        self.title = "calculadora"
        self.theme_cls.primary_palette = "Green"
        return Builder.load_file("main.kv")


MyApp().run()

main.kv (just import the calc class, and add its object to the manager) main.kv (只需导入 calc class,并将其 object 添加到管理器中)

#:kivy 1.0.9
#:import calc calc.calc

NavigationLayout:
    id: nav_layout

    ScreenManager:

        Screen:

            BoxLayout:
                orientation: 'vertical'

                MDToolbar:
                    title: app.title
                    elevation: 10
                    left_action_items: [['menu', lambda x: nav_drawer.set_state()]]

                ScreenManager:
                    id: screen_manager
                    calculator:
                        
                    

    MDNavigationDrawer:
        id: nav_drawer

        BoxLayout:
            orientation: 'vertical'
            padding: '8dp'
            spacing: '8dp'

            Image:
                size_hint: None, None
                size: '280dp', "200dp"
                source: "logo.png"
            
            ScrollView:
            
                MDList:

                    OneLineIconListItem:
                        text: "calculadora"
                        on_release:
                            screen_manager.add_widget(calc())
                            screen_manager.current = 'calc'
                            nav_drawer.set_state()
                        IconLeftWidget:
                            icon: 'view-dashboard'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM