Force Window Size Kivy - python

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.

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)

No name 'ObjectProperty' in module 'kivy.properties' - Python Kivy

When I try to import ObjectProperties from kivy.properties, I get an error:
"No name 'ObjectProperty' in module 'kivy.properties'pylint(no-name-in-module)"
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.config import Config
from kivy.event import EventDispatcher
from kivy.properties import ObjectProperty
Config.set('graphics', 'width', 1000)
Config.set('graphics', 'height', 1000)
Config.set('graphics', 'resizable', False)
class GridContainer(GridLayout):
text_input1 = ObjectProperty()
label1 = ObjectProperty()
def count_sum(self):
self.label1.text = int(self.text_input1.text[0]) + int(self.text_input1.text[1])
class MyApp(App):
def build(self):
GridContainer()
return GridContainer()
Are there any ways to solve this problem?
I found myself in the same situation simply because I thought python only accepts libraries written in python. Python codes/libraries can be made to run faster by compiling them into a mixture of C and Python codes as observed with the properties library located in the kivy directory of site-packages of your python installation folder. Using this method, the compiled python codes can be imported without any troubles just as prior to compilation.
Myself, I have not seen any extension that lints cython codes and they are all built to work with libraries developed with python not cython. Thus, they tend to have problems linting those that have been written/compiled to cython.
As inclement clearly stated, this is not an issue you should worry about if the program runs fine. It is a problem with IDE linting extensions, they cant lint cython codes. You can go with Jayden solution if you dont want to be seeing the squiggy red lines all the time.
I hope this clearifies things..?
I found a workaround:
replace from kivy.properties import ObjectProperty with import kivy.properties as kyprops
type kyprops anytime you need to declare ObjectProperty
Let me know if that helps!
If the code runs fine, this is just a bug in your linter. As already linked by others, it is probably not capable of properly understanding imports from cython-built libraries.

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

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()

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.
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.

Categories