7

How do you change the size of a window using Kivy. I've been searching around and am able to change pretty much everything except the size of the window that things go into.

From the sample pictures file: main.py

#!/usr/bin/kivy
'''
Pictures demo
=============

This is a basic picture viewer, using the scatter widget.
'''

import kivy
kivy.require('1.0.6')

from glob import glob
from random import randint
from os.path import join, dirname
from kivy.app import App
from kivy.logger import Logger
from kivy.uix.scatter import Scatter
from kivy.properties import StringProperty
# FIXME this shouldn't be necessary
from kivy.core.window import Window


class Picture(Scatter):
    '''Picture is the class that will show the image with a white border and a
    shadow. They are nothing here because almost everything is inside the
    picture.kv. Check the rule named <Picture> inside the file, and you'll see
    how the Picture() is really constructed and used.

    The source property will be the filename to show.
    '''

    source = StringProperty(None)


class PicturesApp(App):

    def build(self):

        # the root is created in pictures.kv
        root = self.root

        # get any files into images directory
        curdir = dirname(__file__)
        for filename in glob(join(curdir, 'images', '*')):
            try:
                # load the image
                picture = Picture(source=filename, rotation=randint(-30,30))
                # add to the main field
                root.add_widget(picture)
            except Exception as e:
                Logger.exception('Pictures: Unable to load <%s>' % filename)

    def on_pause(self):
        return True


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

pictures.kv

#:kivy 1.0
#:import kivy kivy
#:import win kivy.core.window

FloatLayout:
    canvas:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            source: 'data/images/background.jpg'
            size: self.size

    BoxLayout:
        padding: 10
        spacing: 10
        size_hint: 1, None
        pos_hint: {'top': 1}
        height: 44
        Image:
            size_hint: None, None
            size: 24, 24
            source: 'data/logo/kivy-icon-24.png'
        Label:
            height: 24
            text_size: self.width, None
            color: (1, 1, 1, .8)
            text: 'Kivy %s - Pictures' % kivy.__version__



<Picture>:
    # each time a picture is created, the image can delay the loading
    # as soon as the image is loaded, ensure that the center is changed
    # to the center of the screen.
    on_size: self.center = win.Window.center
    size: image.size
    size_hint: None, None

    Image:
        id: image
        source: root.source

        # create initial image to be 400 pixels width
        size: 400, 400 / self.image_ratio

        # add shadow background
        canvas.before:
            Color:
                rgba: 1,1,1,1
            BorderImage:
                source: 'shadow32.png'
                border: (36,36,36,36)
                size:(self.width+72, self.height+72)
                pos: (-36,-36)

I'd expect to be able to put a size tag in the FloatLayout or the Canvas in order to resize a window, but it doesn't seem to work.

How do I determine how large the canvas (or containing window -- perhaps i'm searching on the wrong terms) is going to be before the app is run?

-edit- I've found that adding the following in the includes section of the .py works:

from kivy.config import Config 
Config.set('graphics', 'width', '640') 
Config.set('graphics', 'height', '1163')

Is there a way of doing this using just the .kv file?

hackabletype
  • 73
  • 1
  • 5

3 Answers3

8

No, the size cannot be set in kv. You can set it using Config as you do above, or you can change the window size later by setting the size on the Window object:

from kivy.core.window import Window
Window.size = (640, 1163)
kitti
  • 14,663
  • 31
  • 49
  • 1
    Ryan, this works for me just 'some times', I'm returning a Label() from my app build, and some times the window.size changes and sometimes it changes, but it back to default value. What do you thing is going wrong? – Carlos Porta May 03 '16 at 04:57
  • Is there a way to access the window size from within a class? – Daniel Sep 19 '16 at 17:13
  • https://stackoverflow.com/questions/36996162/setting-kivy-window-size-not-working – user584583 Jul 14 '17 at 02:37
5

To manage window config you can use the import from kivy.config import Config on top of source file.

from kivy.config import Config
Config.set('graphics', 'resizable', '0')
Config.set('graphics', 'width', '640')
Config.set('graphics', 'height', '480')
IKavanagh
  • 6,089
  • 11
  • 42
  • 47
sur1c4t4
  • 51
  • 1
  • 2
1

Don't use

from kivy.core.window import Window

which makes these not work

Config.set('graphics', 'width', '200')
Config.set('graphics', 'height', '400')
Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
  • Don't ask me why but if you set Config before you import Window it works. eg:  ```from kivy.config import Config Config.set('graphics', 'width', '500') Config.set('graphics', 'height', '120') Config.set('graphics', 'fullscreen', '0') from kivy.core.window import Window``` I got a hint from [documentation](https://kivy.org/doc/stable/api-kivy.config.html?highlight=config#module-kivy.config) which says `Config.set should be used before importing any other Kivy modules.` – Cyril Waechter Jan 13 '19 at 01:06