Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kivy: How to change window size?

Tags:

python

kivy

I'm starting to write a program using kivy, but I have some problems understand how it deals with sizes.

For example:

import kivy
kivy.require('1.5.1')

from kivy.app import App
from kivy.uix.button import Button

class MyApp(App):
    def build(self): return Button(text='Some text')

MyApp().run()

The above program works, but it creates a huge window. Trying to set size=(100, 100) does not change anything. Setting size_hint=(None, None) will show a button with the correct size, but it is placed randomly inside a still huge window. Trying to set the size of MyApp does not change anything too.

How do I create a window with the same size of the button? It should be a simple enough task, but looking at the documentation and example I can't find anything about this.

like image 536
Bakuriu Avatar asked Dec 23 '12 21:12

Bakuriu


People also ask

How do I make kivy images full screen?

One way of fixing it is to set allow_stretch to True and keep_ratio to False (within the kv string). Moreover I have set the size_hint to None and used the Window class to get the entire screen size. Just tested it with one of my images, hope it works for you as well.

How to import a window in Kivy?

#import everything you want from kivy.core. window import Window #You must import this Window .size = ( 600, 600) # Set it to a tuple with the (width, height) in Pixels # ( 800, 600) is the default #Your usual kivy code....

Is there a way to set the maximum size of Kivy?

SDL2 does expose an api to set the maximum size, it works the same way as setting the minimum size. However, Kivy doesn't currently expose that option. You probably would need to modify Kivy to add it - contributions welcome if you do. Maybe it's possible via the win32 api or something, or other platform-specific methods, I don't know about that.

How do I make a Kivy window fullscreen?

Use kivy.base.EventLoop.dpi instead. Check whether or not the window currently has focus. focus is a read-only AliasProperty and defaults to True. This property sets the fullscreen mode of the window. Available options are: True, False, ‘auto’ and ‘fake’. Check the config documentation for more detailed explanations on these values.

How do I change the default settings of my Kivy app?

Kivy has a configuration file which determines the default settings. In order to change these settings, you can alter this file manually or use the Config object. Configuration options control the initialization of the App.


3 Answers

There're currently two ways:

  • Before the window is created:

    import kivy
    kivy.require('1.9.0')
    
    from kivy.config import Config
    Config.set('graphics', 'width', '200')
    Config.set('graphics', 'height', '200')
    
  • Dynamically after the Window was created:

    from kivy.core.window import Window
    Window.size = (300, 100)
    
like image 82
martin Avatar answered Oct 09 '22 21:10

martin


Use this:

from kivy.core.window import Window
Window.size = (300, 100)

If you use

from kivy.config import Config
Config.set('graphics', 'width', '200')
Config.set('graphics', 'height', '200')
Config.write()

this will lead to loss of default screen size! Default screen size is really useful.

like image 27
SagitSri Avatar answered Oct 09 '22 19:10

SagitSri


I would comment on martin's answer, but I don't have the reputation. When setting the config file, be sure to "write" your changes:

from kivy.config import Config
Config.set('graphics', 'width', '200')
Config.set('graphics', 'height', '200')
Config.write()

It's exactly like committing info to a database, if you know anything about that.

like image 9
Aaron Bell Avatar answered Oct 09 '22 21:10

Aaron Bell