Kivy: How to change window size? - python

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.

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.

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.

Related

Disable fullscreen kivymd

I have been working on some projects with kivy for a while, some of them requiring fullscreen, but now I want to learn kivymd. The problem is that my kivymd app opens in fullscreen, how can I change that from the code? Also, how can I change the default window size of the application?
Set the window size BEFORE importing MD modules and BEFORE importing the kivy.core.window.Window module!
from kivy.config import Config
Config.set('graphics', 'width', '200')
Config.set('graphics', 'height', '200')
[impots MD modules]
[impots kivy.core.window.Window modules]
It is simple resizing the windows but note one thing while packing this for android apk you should remove these lines:
from kivy.core.window import Window
Window.size =[x ,y]
The x and y variables is upto your choice
Add this lines at the top your code. change the values to adjust the size of your window to your desired size.
from kivy.core.window import Window
Window.size = (370, 700)

How to fix KIVY default window on app appearing thin on launch... almost without width?

All of the projects I opened on Kivy open up with a really long but thin window... I have no idea why or how I this happened. Could someone please help me fix this?
Here is a screen shot! Thank you so much
-https://drive.google.com/file/d/17PeHfFJBkMPI-eW4PbO8qzwG2W2IM1hn/view?usp=sharing
You may set the width and height before opening your window:
import kivy
kivy.require('1.11.0')
from kivy.config import Config
# Ensure window is going to be 800x400
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '400')
Alternatively, you can set the window size after creating it with its size attribute, which is a (width, height) tuple:
from kivy.core.window import Window
Window.size = (800, 400)
You can find more configuration options here: https://kivy.org/doc/stable/api-kivy.config.html

Kivy - How to set the window size based off display size?

I have just been starting to use the Kivy framework, and am slightly confused on setting the window size based off the display size of the screen. For example if my display size is 200x100mm, how would I set that in Kivy?
I've looked at examples such as this one from SO, however I can't change seem to alter the size based off of the mm metric.
I have also tried converting mm to pixels, however that is also the wrong size on my screen, and I think it is a frowned upon technique from what I have read online.
import kivy
kivy.require('1.9.1')
from kivy.config import Config
Config.set('graphics', 'width', '200')
Config.set('graphics', 'height', '100')
I would like to add 200mm and 100mm in, however that brings up errors when attempting to build:
[CRITICAL] [Window ] Unable to find any valuable Window provider at all!
sdl2 - ValueError: invalid literal for int() with base 10: '200mm'
How should I go about doing this in a correct manner?
I don't think that the size in mm counts, only the resolution of that screen.
So try to find out which resolutions will be in your target devices and tests them:
Config.set('graphics', 'width', '200')
Config.set('graphics', 'height', '100')
If just you want the full screen feature, Try this:
Window.fullscreen = 'auto' # you can also try True...

Force Window Size Kivy

I don't find how to force the window size with the kivy language. (ex: 800x600p)
Can you help me ?
Thanks for reading.
PITO2901
The KV language does not have the ability to configure window settings, but it can be done easily in your 'main.py' file by using the Config object from kivy.config. Just make sure to put any configuration you do before your other imports. Otherwise, they won't work.
# configuration
from kivy.config import Config
Config.set('graphics', 'width', 800)
Config.set('graphics', 'height', 600)
# other imports
from kivy.app import App
from kivy.uix.widget import Widget
...
Set kivy.core.window.Window.size to (800, 600).
You can't set it directly in kv, but you could bind it to a button or similar. You would also need to import the Window to kv, using the #:import statement explained in the doc.

Issue setting Kivy to fullscreen

I'm trying to write an application that runs kivy at full screen. But these are my issues:
1) When I run the command:
#Config.set('graphics', 'fullscreen', 1)
Then kivy appears to go full time, but the window has a lot of black spaces around the background image. Even if I elongate the image, kivy just cuts the image when showing it.
2) When I run this command to set the window size to the size of my screen:
Config.set('graphics', 'width', '1366')
Config.set('graphics', 'height', '768')
This way actually gives me a better result than full screen, but kivy returns a height parameter of only 715 instead of the 768, which is the value I told kivy to use (as you can see in the Config.set() function above).
My screen resolution is 1366x768
How can I solve this issue and make my kivy app go real full screen?
Thank you very much
Had a similar problem. Using the 'auto' option got rid of the bands for me.
Window.fullscreen = 'auto'
Quote from Kivy Configuration Object documentation: "If set to auto, your current display’s resolution will be used instead. This is most likely what you want."
Try
from kivy.core.window import Window
Window.fullscreen = True
Do this before you App.run() method, and it should switch to fullscreen mode.
Cheers
I managed to do it as follows:
from kivy.core.window import Window
Window.maximize()
I just want to complement:
from kivy.core.window import Window
Window.size = (1366, 768)
Window.fullscreen = True
this works for me:
Config.set('graphics', 'fullscreen', 'auto')
Answering lately For those who are still struggling to figure out how to have the true fullscreen. I have managed to get the rid of those black strip by adding Config.set('graphics','window_state'_'maximized' just after the fullscreen call. the whole code looks like
from kivy.config import Config
# ...
if __name__ == "__main__":
Config.set('graphics', 'fullscreen', 'auto')
Config.set('graphics', 'window_state', 'maximized')
Config.write()
YourApp().run()
This will work no matter what is your resolution.
from kivy.core.window import Window
Window.fullscreen = True
Window.maximize()

Categories