【问题标题】:Using ScreenManager with separate py and kv files for each screen将 ScreenManager 与每个屏幕的单独 py 和 kv 文件一起使用
【发布时间】:2021-01-01 18:57:53
【问题描述】:

我正在制作一个计算器应用程序,我希望能够为每个屏幕使用单独的 kv 和 py 文件,我还没有找到任何关于此的文档或教程,the only answer I have found 没有给出代码示例所以我不确定如何实现它。此外,calc.kv 上的按钮通过在 root 中引用它来向 calc.py 发送回调,这样可能会导致问题,因为 root 是 screenmanager

如果您尝试从工具栏中打开计算器,它将返回此错误消息:

 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".

代码如下:

calc.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

#: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)            
            

ma​​in.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'

ma​​in.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()

提前致谢。

【问题讨论】:

  • 您尚未使用名称calc 定义任何Screen

标签: python kivy kivymd


【解决方案1】:

您需要为 calc.kv 文件中的 &lt;calc&gt; 命名,而不是从 Widget 类继承,而是从 calc.py 文件中的 Screen 类继承。之后,您需要使用 Builder.load_file("calc.kv") 在 main.py 中加载 calc.kv 文件,然后使用 on_release 之类的函数在 .kv 中添加 calc() Screen

calc.py(只需在此处将继承从 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(只需给类命名以供参考)

#: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)

ma​​in.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()

ma​​in.kv(只需导入 calc 类,并将其对象添加到管理器中)

#: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'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 2023-01-24
    • 2020-01-22
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多