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.
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.
#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....
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.
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.
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.
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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With