Search code examples
pythonkivykivy-language

Screen management and loading functions in kivy PYTHON


So, I'm doing this GUI that has the main screen and a loading screen. The loading screen is used so that the user can see the progress of the application while the program executes a function. I want the program to change to execute the function and switch back to the main screen once the functions are executed. Here is part of my code that you can use to run the program on your end.

# Imports
import kivy
from kivy.app import App    # Imports functions to build the app
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.image import Image
import threading
import time

kivy.require('1.10.1')


def function1():
    time.sleep(5)
    print("Done")


def function2():
    time.sleep(5)
    print("Done 2")


class MainScreen(Screen, FloatLayout):  # Main Screen Class that holds every method in the Start Screen

    auction_name = " "
    default = "Select an auction"
    selected_auction = StringProperty('Select an auction')
    x_value = 450
    y_value = 500

    def change_label(self, change):  # Changes the text that shows what auction is selected

        strtst = "Selected: " + change

        if self.selected_auction == self.default and (change == "Copart" or change == "IAAI"):
            self.selected_auction = "Selected: " + change
            self.selected_auction = "Selected: " + change
        elif self.selected_auction == strtst or self.selected_auction == "Main":
            self.selected_auction = self.default
        elif change != self.selected_auction:
            if self.selected_auction == "Selected: Copart and IAAI":
                self.selected_auction = "Selected: " + change
            else:
                if change == "None":
                    self.selected_auction == "TO START, PLEASE SELECT AN AUCTION"
                else:
                    self.selected_auction = "Selected: Copart and IAAI"

    def change_screen(self):
        self.manager.current = "Loading Screen"

    t2 = threading.Thread(target=function1)
    t3 = threading.Thread(target=function2)

    def start_process(self):  # Used to start the web scraping
        if self.selected_auction == 'Selected: Copart':
            self.manager.current = "Loading Screen"
            self.t3.start()
            self.manager.current = "Results Screen"
            self.selected_auction = "Select an auction"
        elif self.selected_auction == 'Selected: IAAI':
            self.manager.current = "Loading Screen"
            self.t2.start()
            self.selected_auction = "Select an auction"
        elif self.selected_auction == 'Selected: Copart and IAAI':
            self.manager.current = "Loading Screen"
            self.t2.start()
            self.t3.start()
        else:
            self.change_label("None")


class LoadingScreen(Screen):

    def cancel(self):
        box = BoxLayout(orientation='vertical')
        padding = Label(text= "If you say yes all progress will be lost!")
        buttons = BoxLayout()
        warning = Image(source="Data/warning.png")
        yes_button = Button(text= "Yes, do cancel it.", on_release=lambda x:self.back(),
                        on_press=lambda *args: pop.dismiss(), size=(250, 120), background_color=(300, 0.5, 0, .7))
        no_button = Button(text="No, don't cancel.", on_press=lambda *args: pop.dismiss(), size=(250, 120),
                       background_color=(.25, 150, .04, 0.5))
        box.add_widget(warning)
        box.add_widget(padding)
        buttons.add_widget(yes_button)
        buttons.add_widget(no_button)
        box.add_widget(buttons)

        pop = Popup(title="Do you wish to cancel?", content=box, size_hint=(None, None), size= (800, 500))
        pop.open()


class Manager(ScreenManager):

    main_screen = ObjectProperty(None)
    loading_screen = ObjectProperty(None)


class MainApp(App):

    choice = ""
    source = StringProperty(None)

    def build(self):
        m = Manager(transition=NoTransition())
        return m


if __name__ == "__main__":
    Window.clearcolor = (.46875, .46875, .4765, 1)
    Window.size = (850, 1000)
    MainApp().run()

In the Main Screen, there is a button that when pressed should change the screen and executed the function, which is handled using start_process(). Here is my main.kv file:

#: import NoTransition kivy.uix.screenmanager.NoTransition
#: import ProgressBar kivy.uix.progressbar
#: import Widget kivy.uix.widget
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton
#: import Popup kivy.uix.popup
#: import main gui_test

<Manager>:
    id: screen_manager

    main_screen: mainS
    loading_screen: loadingS

    MainScreen:
        id: mainS
        name: "Main Screen"
        manager: screen_manager

    LoadingScreen:
        id: loadingS
        name: "Loading Screen"
        manager: screen_manager


<MainScreen>
    FloatLayout:

        # Start Button
    Button:
        on_release: root.start_process()
        text: "Start"
        font_size: 16
        size_hint: 0.6, 0.12
        pos: root.center_x - (self.width/2), root.y + self.height
        background_normal: ''
        background_down:
        background_color: .25, .75, .04, 1
        font_size: 50

    # COPART BUTTON
    Button:
        size_hint: 0.279, 0.3
        pos: root.x + (0.3 * self.width), root.top - self.height - 160
        on_press: root.change_label("Copart")

    # IAAI BUTTON
    Button:
        id: IAAI_Button
        size_hint: 0.27, 0.3
        pos: root.x + (1.7 * self.width) + 300, root.top - self.height - 160
        on_press: root.change_label("IAAI")

    Label:
        text: root.selected_auction
        font_size: 50

<LoadingScreen>
    FloatLayout:

        Button:
            text: "Cancel"
            size_hint:0.30, 0.1
            pos: root.center_x - (self.width/2), 100
            background_color: 300, 0.5, 0, .4
            on_press: root.cancel()

Do you have any suggestions on what I'm doing wrong? Thank you for your help!


Solution

  • Here is one way to change back to the main screen after running a function. Modify the target function as:

    def function1():
        time.sleep(5)
        print("Done")
        Clock.schedule_once(App.get_running_app().show_main_screen)
    
    
    def function2():
        time.sleep(5)
        print("Done 2")
        Clock.schedule_once(App.get_running_app().show_main_screen)
    

    The Clock.schedule_once() schedules something to be run on the main thread (required for changes to the UI).

    And modify the MainApp as:

    class MainApp(App):
    
        choice = ""
        source = StringProperty(None)
    
        def build(self):
            self.m = Manager(transition=NoTransition())  # save a reference to `Manager`
            return self.m
    
        def show_main_screen(self, dt):
            self.m.current = "Main Screen"  # use the saved reference to change the current screen
    

    Also, in your 'kv' file, the lines:

    manager: screen_manager
    

    are not needed. That property is automatically added to each Screen.