Fixed window size for Kivy programs

大兔子大兔子 提交于 2020-05-08 04:08:53

问题


Is there any way to keep the window size for Kivy programs fixed? Fixed in the sense its window size cannot be increased or decreased. For example, I want a Kivy program whose window size is 500x500 and the end user cannot either change the window size or turn it into fullscreen. I tried setting the height, width, minimum_height and minimum_width all with same values and still I can change window size and fullscreen it.


回答1:


There is a way to configure the app to disable resizing

from kivy.config import Config
Config.set('graphics', 'resizable', False)

Also, the same way you can set the default width-height of the window.
Keep something in mind. Doing it like that in the beginning of your app, it will keep the settings only for that app. However, if you then run a Config.write(), you'll save the settings in a configuration file.

Config.set should be used before importing any other Kivy modules. Ideally, this means setting them right at the start of your main.py script. Alternatively, you can save these settings permanently using Config.set then Config.write. In this case, you will need to restart the app for the changes to take effect. Note that this approach will effect all Kivy apps system wide.

Read this wiki article for more info.




回答2:


There are actually a bunch of ways you can do this, that said many of them are dependent on how you're writing your code and since you've not given us an example I can only show you basic examples...

Say for instance your'e not using the kivy deign language and you're doing your project in straight python you could for instance setup a Root widget in the following way

Root = Widget(size = (500,500))

You could also avoid that and simply do for instance

Window.size = (500, 500)

Now if you're using the kv design language it's just eas easy except you'd be setting the size of your canvas, Rectangles etc.. inside the .kv file.

And as mentioned by @Leva7 you could also use for instance

from kivy.config import Config
Config.set('graphics', 'resizable', '0') #0 being off 1 being on as in true/false
Config.set('graphics', 'width', '500')
Config.set('graphics', 'height', '500')

Note that the above (Ie, Config.set()) should be placed at the top of the source code near the import section!




回答3:


You can give like this on kivy-1.10.0+

import kivy
from kivy.app import App
from kivy.core.window import Window
from kivy.config import Config
kivy.config.Config.set('graphics','resizable', False)



class MyApp(App):
    def build(self):
        Window.size = (1280,720)
MyApp().run()



回答4:


The above stated configurations dont work for the RPi by default.

Window.size(500,500)

takes no effect. If someone needs to disable the fullscreen and want to have full control over the screen size on his RPi:

disable kivy fullscreen

Steps to do:

sudo raspi-config 

Advanced Options ->
GL Driver ->
G1 GL (Full KMS)->
OK ->
Reboot

Tested on RPi 3B Stretch with Python3.5



来源:https://stackoverflow.com/questions/37164410/fixed-window-size-for-kivy-programs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!