Python Kivy:如何改变窗口大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14014955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:10:57  来源:igfitidea点击:

Kivy: How to change window size?

pythonkivy

提问by Bakuriu

I'm starting to write a program using kivy, but I have some problems understand how it deals with sizes.

我开始使用 编写程序kivy,但我在理解它如何处理大小方面存在一些问题。

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 hugewindow. 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 stillhuge window. Trying to set the size of MyAppdoes not change anything too.

上面的程序可以工作,但它创建了一个巨大的窗口。尝试设置size=(100, 100)不会改变任何东西。设置size_hint=(None, None)将显示一个大小正确的按钮,但它随机放置在一个仍然很大的窗口中。尝试设置 的大小MyApp也不会改变任何东西。

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.

如何创建与按钮大小相同的窗口?这应该是一个足够简单的任务,但是查看文档和示例我找不到任何关于此的信息。

回答by martin

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)
    
  • 在创建窗口之前:

    import kivy
    kivy.require('1.9.0')
    
    from kivy.config import Config
    Config.set('graphics', 'width', '200')
    Config.set('graphics', 'height', '200')
    
  • 创建窗口后动态:

    from kivy.core.window import Window
    Window.size = (300, 100)
    

回答by Aaron Bell

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.

如果您对此有所了解,这就像将信息提交到数据库一样。

回答by SagitSri

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.

这将导致默认屏幕尺寸丢失!默认屏幕尺寸非常有用。