54

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.

Totem
  • 7,189
  • 5
  • 39
  • 66
Bakuriu
  • 98,325
  • 22
  • 197
  • 231

4 Answers4

117

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)
    
user
  • 5,370
  • 8
  • 47
  • 75
martin
  • 93,354
  • 25
  • 191
  • 226
  • This works perfectly, I was having this exact same problem and could not find anything to help me, fortunately i found this... thanks so much Martin – Cid-El Jul 15 '16 at 07:26
  • Is there a way to make the window fit to the whole screen? regardless of resolution? – Yair Oct 11 '16 at 10:13
  • @Yair You mean fullscreen or windowed but with the size of the screen? – martin Oct 11 '16 at 10:17
  • Sorry, I don`t really see the difference. I guess I mean full screen. – Yair Oct 11 '16 at 10:22
  • Ok, now I can see the difference. I actually meant Windowed size. Was looking at https://kivy.org/docs/api-kivy.core.window.html but couldn`t find the right command – Yair Oct 11 '16 at 11:37
20

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.

SagitSri
  • 491
  • 6
  • 15
  • 1
    It could be a good idea to explain why it's "really useful". – PhoneixS Nov 14 '22 at 11:03
  • Um, if you want to maintain the "default" screen size - DON'T use `Config.write()` - it's not needed for runtime changes. Be sure you set the size before the line `from kivy.app include App` of for KivyMD `from kivymd.app include MDApp` – Android Control Aug 26 '23 at 14:44
10

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.

Aaron Bell
  • 852
  • 2
  • 11
  • 26
1

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.

Pyzard
  • 451
  • 3
  • 14