55

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.

4 Answers 4

120

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)
    
5
  • 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, 2016 at 7:26
  • Is there a way to make the window fit to the whole screen? regardless of resolution?
    – Yair
    Oct 11, 2016 at 10:13
  • @Yair You mean fullscreen or windowed but with the size of the screen?
    – martin
    Oct 11, 2016 at 10:17
  • Sorry, I don`t really see the difference. I guess I mean full screen.
    – Yair
    Oct 11, 2016 at 10:22
  • Ok, now I can see the difference. I actually meant Windowed size. Was looking at kivy.org/docs/api-kivy.core.window.html but couldn`t find the right command
    – Yair
    Oct 11, 2016 at 11:37
21

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.

2
  • 1
    It could be a good idea to explain why it's "really useful".
    – PhoneixS
    Nov 14, 2022 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 Aug 26, 2023 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.

4
  • 3
    Config.write() makes the changes permanent. They will be used as default then.
    – Holger
    Jan 27, 2018 at 18:24
  • 1
    To make it clear, this make it the default for all kivy applications which I don't know if it is a good idea.
    – PhoneixS
    Nov 14, 2022 at 11:06
  • "It's exactly like committing info to a database, ..."exactly or like?
    – MarianD
    May 8, 2023 at 15:37
  • We'll go with "like" :3
    – Aaron Bell
    Aug 2, 2023 at 23:46
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.

1
  • 1
    This is more a comment than an answer, please don't post comments as answers.
    – PhoneixS
    Nov 14, 2022 at 11:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.