Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...