簡體   English   中英

將 ScreenManager 與每個屏幕的單獨 py 和 kv 文件一起使用

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

我正在制作一個計算器應用程序,我希望能夠為每個屏幕使用單獨的 kv 和 py 文件,我沒有找到任何關於此的文檔或教程,我找到的唯一答案沒有給出代碼示例所以我不確定如何實現它。 此外,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".

這是代碼:

計算.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()

計算.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)            
            

主文件

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

主文件

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.kv 文件中的<calc>命名,而不是從Widget class 繼承,而是從 calc.py 文件中的Screen class 繼承。 之后,您需要使用Builder.load_file("calc.kv")在 your.kv 中添加calc() Screen ,如on_release

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 (只需給 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 (只需在開始時加載 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 (只需導入 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