Mahdi
Mahdi

Reputation: 133

Changing size of two different kivy screens

I have two screens in a Kivy program. The first one represents the login screen, so it needs to be smaller, while the other has the data, so I need to make it full screen.

I tried to use

from kivy.config import Config
Config.set('graphics', 'width', '350')
Config.set('graphics', 'height', '250')

But, the problem is with this the other screen's size has also decreased, help me if you have any idea what I need to do to have different sizes for different screens, thanks.

Upvotes: 1

Views: 1231

Answers (2)

RufusVS
RufusVS

Reputation: 4127

I answered almost the identical question earlier today. Code your python similar to this:

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window

class Screen_Manager(ScreenManager):
    pass

class Main(Screen):
    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)

    def on_pre_enter(self):
        Window.size = (900, 500)     

class Login(Screen):
    def __init__(self, **kwargs):
        super(Login, self).__init__(**kwargs)

    def on_pre_enter(self):
        Window.size = (400, 300)


class MultiScreenApp(App):
    def build(self):
        return Screen_Manager()

MultiScreenApp().run()

with a multiscreen.kv file similar to this:

<Screen_Manager>:
    id: screen_manager
    Login:
    Main:

<Login>:
    name: 'login'
    Button:
        text: 'Go to Main'
        on_press: root.manager.current = 'main'

<Main>:
    name: 'main'
    Button:
        text: 'Go to Login'
        on_press: root.manager.current = 'login'

Upvotes: 1

Amin Etesamian
Amin Etesamian

Reputation: 3699

Define a method like

from kivy.core.window import Window

def update_window_size(width, height):
    # Validate width and height then
    Window.size = (width, height)

and call it with the desired height and width anywhere you want.

___ Edit 1 - Adding sample. Change Screen size in the Screen's initilization

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window

sm = ScreenManager()


class Main(Screen):
    def __init__(self):
        super(Main, self).__init__()


class Login(Screen):
    def __init__(self):
        super(Login, self).__init__()
        # Change login screen size in it's __init__
        update_window_size(250, 250)


def update_window_size(width, height):
    # Validate width and height then
    Window.size = (width, height)


class MyApp(App):
    def build(self):
        return sm

MyApp().run()

Upvotes: 0

Related Questions