6

I follwed this stackoverflow question, but neither alternatives worked.

This is my code:

from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window

class MyApp(App):
    def build(self):
        return Label(text='text')

if __name__ == '__main__':
    Window.size = (1366, 768)
    MyApp().run()

Sometimes the size works, Kivy creates a screen with size 800x600 then changes it to 1366x768. And sometimes Kivy creates a screen with size 800x600 then changes it to 1366x768, but then back to 800x600.

And if I change my code to:

from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window

from kivy.config import Config
Config.set('graphics', 'width', '200')
Config.set('graphics', 'height', '200')

class MyApp(App):
    def build(self):
        return Label(text='text')

if __name__ == '__main__':
    MyApp().run()

With this code, nothing happens on my screen. I'm using Kivy v1.9.2-dev0. What I should do to fix it?

Community
  • 1
  • 1
Carlos Porta
  • 1,224
  • 5
  • 19
  • 31
  • It worked for me a few days ago. Try if something smaller works. Isn't there any message in log? – Peter Badida May 03 '16 at 06:35
  • No, there is no difference on log when it works and not. =( – Carlos Porta May 03 '16 at 07:12
  • There should be a message like `[DEBUG] [Window] Resize window to (x, y)` if it's resized correctly. Is there any other message? The best would be to paste the whole log here and other info such as OS, 32/64, window provider, etc. And from that size I guess you want to achieve fullscreen, therefore try to use `Window.fullscreen = True` too. – Peter Badida May 03 '16 at 09:29
  • 1
    This my log, https://bpaste.net/show/3105907ff5a3. I'm using Gentoo Linux - 64bits, xfce default window manage and fullscreen works. – Carlos Porta May 03 '16 at 11:40
  • Mayve pygame doesn't work well with master branch. Try sdl2, the default provider now. – Peter Badida May 03 '16 at 11:43
  • The inclement solution worked! – Carlos Porta May 03 '16 at 11:45

3 Answers3

15

Put the Config settings before all the other imports - it's too late after importing Window, as the config has already been accessed to determine its initial size and your new settings are ignored.

inclement
  • 29,124
  • 4
  • 48
  • 60
  • Thank you inclement, it worked. Do know why changing size using Window works just sometimes? – Carlos Porta May 03 '16 at 11:44
  • Sounds like a race condition, I'm not sure why exactly it happens but it's probably to do with lots of things happening when the app is first started. – inclement May 03 '16 at 11:54
6

Make your window non-resizable

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

set the window size

from kivy.core.window import Window
Window.size = (600, 500)

see example at https://kivyapps.wordpress.com/video-streaming-using-kivy-and-python/

Atul
  • 546
  • 4
  • 16
0
def on_size(self,*args):#this funciton is written becase default size of canvas is 100*100 when done from code
    #print("on size : "+ str(self.width) +","+str(self.height))
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jul 25 '23 at 00:22