Mrhankycowdog
Mrhankycowdog

Reputation: 1

Python-Kivy: Change screens using Python method (nested Screen managers created in Kivy code)

I am new-ish to Kivy, but have created an inventory program using Tkinter.

I am making an Inventory Program using Kivy and plan on having a login screen first with a TextInput box for the username (only), and with the on_text_verify I would like it eventually to run a method that checks the username against a database, but for now I am trying just to get it to run a method (in Python code) that changes the screen to one which contains a nested ScreenManager.

I know how to use KV code to make it change screens, but since I will need to check the username against a database I will need it to instead run a Python method which does so and then, if the username is accepted, then change the screen. Here is what I have so far:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
## I have imported all of these as I have been trying out different things ##

class LoginScreen(Screen):
    pass
class MainScreen(Screen):
    pass
class ScanScreen(Screen):
    pass
class InventoryScreen(Screen):
    pass
class ItemsScreen(Screen):
    pass
class LogsScreen(Screen):
    pass
class AlertsScreen(Screen):
    pass
class HelpScreen(Screen):
    pass


class MainWindow(FloatLayout):


    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)

    def goto_Mainscr(self):
        self.current="Mainscr"


class Invtest3App(App):
    def build(self):
        return MainWindow()


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

Here is the KV code:

<MainWindow>:
    orientation: 'vertical'
    name: "mainw"
    id: mw
    ScreenManager:
        id: loginsm
        LoginScreen:
        MainScreen:

<ScanScreen>:
    name: "Scan"
    Button:
        text: "Scan In"
        pos_hint: {"x": 0, 'y': 0}
        size_hint: 1.0/5.0, 0.1
    Button:
        text: "Scan Out"
        pos_hint: {"x": 1.0/5.0, 'y': 0}
        size_hint: 1.0/5.0, 0.1
    Button:
        text: "Event"
        pos_hint: {"x": 2.0/5.0, 'y': 0}
        size_hint: 1.0/5.0, 0.1
    Button:
        text: "Recount"
        pos_hint: {"x": 3.0/5.0, 'y': 0}
        size_hint: 1.0/5.0, 0.1
    Button:
        text: "Logout"
        pos_hint: {"x": 4.0/5.0, 'y': 0}
        size_hint: 1.0/5.0, 0.1
        on_press: app.root.ids.loginsm.current = 'Loginscr'

<InventoryScreen>:
    name: "Inventory"
    Label:
        text: "Inventory Screen"

<ItemsScreen>:
    name: "Items"
    Label:
        text: "Items Screen"

<LogsScreen>:
    name: "Logs"
    Label:
        text: "Logs Screen"

<AlertsScreen>:
    name: "Alerts"
    Label:
        text: "Alerts Screen"

<HelpScreen>:
    name: "Help"
    Label:
        text: "Help Screen"

<LoginScreen>:
    name: "Loginscr"
    id: loginscr
    Label:
        name: "loginlabel"
        text: "Scan ID Barcode or Enter V# and Press Enter"
        pos_hint: {"center_x": .5, "center_y": .5}
    TextInput:
        hint_text: "Scan ID Barcode or Enter V# and Press Enter"
        id: login_input
        size_hint: 0.2, 0.1
        pos_hint: {"center_x": .5, "center_y": 0.2}
        multiline: False
        focus: True
        on_text_validate: app.root.goto_Mainscr; self.text=""
        #app.root.goto_Mainscr
        #app.root.ids.loginsm.current = "Mainscr"

<MainScreen>:
    name: "Mainscr"
    FloatLayout:
        pos_hint: {'top': 1}
        size_hint: 1, 0.1
        Button:
            name: "scanButton"
            text: "Scan"
            pos_hint: {'x': 0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Scan"
        Button:
            name: "inventoryButton"
            text: "Inventory"
            pos_hint: {'x': 1.0/6.0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Inventory"
        Button:
            name: "itemsButton"
            text: "Items"
            pos_hint: {'x': 2.0/6.0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Items"
        Button:
            name: "logsButton"
            text: "Logs"
            pos_hint: {'x': 3.0/6.0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Logs"
        Button:
            name: "alertsButton"
            text: "Alerts"
            pos_hint: {'x': 4.0/6.0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Alerts"
        Button:
            name: "helpButton"
            text: "Help"
            pos_hint: {'x': 5.0/6.0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Help"
    FloatLayout:
        ScreenManager:
            id: sm
            pos_hint: {'top': 0.9}
            size_hint: 1, 0.9
            ScanScreen:
            InventoryScreen:
            ItemsScreen:
            LogsScreen:
            AlertsScreen:
            HelpScreen:

Upvotes: 0

Views: 1062

Answers (1)

Qback
Qback

Reputation: 4928

If you want to manage screens without using kv then it looks like this:

  1. Import needed classes:

    from kivy.uix.screenmanager import ScreenManager, Screen
    
  2. Create a screen manager variable:

    scene=ScreenManager()
    
  3. Create a screen:

    screen=Screen(name="login_screen")
    
  4. Create second screen:

    second_screen=Screen(name="main_screen")
    

Now you can change screens like this:

scene.current="login_screen"
scene.current="main_screen"

And you can add widgets to those screens.

Upvotes: 1

Related Questions