Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

use of ScreenManager in Kivy file and python file

I'm new to Kivy, I'm working on a tracking app for vehicle repair and I've got a list of appointments and I'm trying to go to a different screen when you press an item on the list and I I have no clue on why it isn't working for me as it says that it is on a different screen but it isn't on the app

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivymd.uix.list import ThreeLineAvatarIconListItem
from kivymd.uix.list import ImageLeftWidget
from kivy.uix.screenmanager import Screen, ScreenManager, SwapTransition, CardTransition
from kivy.core.window import Window
import sqlite3

DatabaseConnection = sqlite3.connect('CarWorkshopDB.db')
cursor = DatabaseConnection.cursor()

Window.size= (400, 650)

class MainScreen(Screen):
    pass

class AddScreen(Screen):
    pass

class DetailsScreen(Screen):
    pass

class WorkApp(MDApp):
    def build(self):
        self.screen_manager = ScreenManager(transition=CardTransition())
        self.screen_manager.add_widget(MainScreen(name='MainScreen'))
        self.screen_manager.add_widget(AddScreen(name='AddScreen'))
        self.screen_manager.add_widget(DetailsScreen(name='DetailsScreen'))
        screen = Builder.load_file("test2.kv")
        return screen

    def on_start(self):
        client = 'John smith'
        cursor.execute("SELECT TrackingNumber, Plate, Status FROM Appointments WHERE Customer =?", (client,))
        for i in cursor:
            self.new_message(i[0], i[1], i[2])
    
    def new_message(self, TrackingNumber, Vehicle, Process):
        new_message = ThreeLineAvatarIconListItem(text=TrackingNumber, secondary_text=Vehicle, tertiary_text=Process)
        new_message.add_widget(ImageLeftWidget(source='RepairIMG.jpg'))
        new_message.bind(on_release=self.on_touch_down)
        self.root.ids.list.add_widget(new_message)
        
    def on_touch_down(self, x):
        print(self.screen_manager.current)
        self.screen_manager.current = "DetailsScreen"
        self.screen_manager.transition.direction = 'right'
        print(self.screen_manager.current)
WorkApp().run()

this is the list code in the kv file

    NavigationLayout:
        ScreenManager:
            id: screen_manager
            MainScreen:
                name : "MainScreen"
                BoxLayout:
                    orientation:'vertical'
                    MDBottomNavigation:
                        panel_color: 1, .643, 0, 1
                        MDBottomNavigationItem:
                            name: 'screen 1'
                            text: 'Home'
                            icon: 'alpha-h-circle'
                            MDToolbar:
                                title: 'Add Repair'
                                md_bg_color: 1, .643, 0, 1
                                elevation: 6
                                pos_hint: {'top':1}
                            Widget:
                        MDBottomNavigationItem:
                            name: 'screen 2'
                            text: 'Track'
                            icon: 'alpha-t-circle'
                            MDToolbar:
                                id: toolbar
                                title: 'Track History'
                                md_bg_color: 1, .643, 0, 1
                                elevation: 6
                                pos_hint: {'top':1}
                            NavigationLayout:
                                x: toolbar.height
                                size_hint_y: 1.0 - toolbar.height/root.height
                                ScreenManager:
                                    Screen:
                                        name: 'ScrollViewScreen'
                                        ScrollView:
                                            MDList:
                                                id: list
                            MDFloatingActionButton:
                                icon: 'plus'
                                md_bg_color: 1, .643, 0, 1
                                on_release: 
                                    screen_manager.current = 'AddScreen'
                                    screen_manager.transition = CardTransition()
                                    screen_manager.transition.direction = "down"
                                pos_hint: {'x': .84, 'y': .02}
                        MDBottomNavigationItem:
                            name: 'screen 4'
                            text: 'More'
                            icon: 'dots-horizontal'
                            MDToolbar:
                                title: 'More'
                                md_bg_color: 1, .643, 0, 1
                                elevation: 6
                                pos_hint: {'top':1}
                            Widget:
                            MDLabel:
                                text: 'dfsfdsf'
                                halign: 'center'
                    
            AddScreen:
                name: 'AddScreen'
                id: Add
                FloatLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        id: toolbar2
                        title: 'Add Tracking Number'
                        md_bg_color: 1, .643, 0, 1
                        elevation: 6
                        pos_hint: {'top':1}
                    MDIconButton:
                        icon : 'close'
                        pos_hint : {'top':0.98, 'x':0.85}
                        on_release : 
                            screen_manager.current = "Main"
                            screen_manager.transition = CardTransition()
                            screen_manager.transition.direction = "up"
                    FloatLayout:
                        x: toolbar2.height
                        size_hint_y: 1.0 - toolbar.height/root.height
                        ScatterLayout:
                            MDLabel:
                                text: 'Track Repair'
                                font_style: 'H4'
                                valign: 'top'
                                pos_hint: {'top':1}
                                text_size: self.size
                                size_hint: None, None
                            MDTextFieldRound:
                                icon_left: 'alpha-t-circle'
                                hint_text: 'Tracking Number'
                                normal_color: 1, .643, 0, 1
                                pos_hint: {'y':.7}
                                text_size: self.size
                            MDTextFieldRound:
                                icon_left: 'alpha-p-circle'
                                hint_text: 'Postcode'
                                normal_color: 1, .643, 0, 1
                                pos_hint: {'y':.6}
                                text_size: self.size
                            MDTextButton:
                                text: "Cant find your tracking number?"
                                custom_color: 1, .643, 0, 1
                                pos_hint: {'y':.5}
                                halign: 'center'
                            MDRectangleFlatButton:
                                text: 'confirm'
                                pos_hint: {'y':.4}
                                valign: 'middle'
                                text_size: self.size
            
            DetailsScreen:
                name: 'DetailsScreen'
                id: Details
                BoxLayout:
                    orientation: 'vertical'
                    MDLabel:
                        text: 'details Screen?'
question from:https://stackoverflow.com/questions/65911767/use-of-screenmanager-in-kivy-file-and-python-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that in your build() method you are building a widget tree in the lines:

    self.screen_manager = ScreenManager(transition=CardTransition())
    self.screen_manager.add_widget(MainScreen(name='MainScreen'))
    self.screen_manager.add_widget(AddScreen(name='AddScreen'))
    self.screen_manager.add_widget(DetailsScreen(name='DetailsScreen'))

But this widget tree is not used in your GUI. The widget tree in your GUI is built from the kv file by:

screen = Builder.load_file("test2.kv")

When your code tries to change the current Screen by using:

self.screen_manager.current = "DetailsScreen"

it is changing the current Screen of a ScreenManager that is not in your GUI.

A fix is to assign the correct value to self.screen_manager, like this:

class WorkApp(MDApp):
    def build(self):
        screen = Builder.load_file("test2.kv")
        self.screen_manager = screen.ids.screen_manager
        return screen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...