Bakuriu
Bakuriu

Reputation: 102039

Kivy: How to change window size?

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.

Upvotes: 57

Views: 98267

Answers (4)

Pranav
Pranav

Reputation: 491

Using Config.write() will set your changes to the default settings. If you accidentally run this and want to revert the changes, you can either run Config.write() again with the default values (therefore reverting your changes) or you can open up $HOME/.kivy/config.ini with a text editor and edit it.

Upvotes: 2

SagitSri
SagitSri

Reputation: 521

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.

Upvotes: 21

Aaron Bell
Aaron Bell

Reputation: 862

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.

Upvotes: 10

martin
martin

Reputation: 96969

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)
    

Upvotes: 122

Related Questions