【问题标题】:how do you change the canvas or window size of an app using kivy如何使用 kivy 更改应用程序的画布或窗口大小
【发布时间】:2014-09-19 05:06:12
【问题描述】:

如何使用 Kivy 更改窗口的大小。我一直在四处寻找,几乎可以更改所有内容,除了进入的窗口大小。

来自示例图片文件: main.py

#!/usr/bin/kivy
'''
Pictures demo
=============

This is a basic picture viewer, using the scatter widget.
'''

import kivy
kivy.require('1.0.6')

from glob import glob
from random import randint
from os.path import join, dirname
from kivy.app import App
from kivy.logger import Logger
from kivy.uix.scatter import Scatter
from kivy.properties import StringProperty
# FIXME this shouldn't be necessary
from kivy.core.window import Window


class Picture(Scatter):
    '''Picture is the class that will show the image with a white border and a
    shadow. They are nothing here because almost everything is inside the
    picture.kv. Check the rule named <Picture> inside the file, and you'll see
    how the Picture() is really constructed and used.

    The source property will be the filename to show.
    '''

    source = StringProperty(None)


class PicturesApp(App):

    def build(self):

        # the root is created in pictures.kv
        root = self.root

        # get any files into images directory
        curdir = dirname(__file__)
        for filename in glob(join(curdir, 'images', '*')):
            try:
                # load the image
                picture = Picture(source=filename, rotation=randint(-30,30))
                # add to the main field
                root.add_widget(picture)
            except Exception as e:
                Logger.exception('Pictures: Unable to load <%s>' % filename)

    def on_pause(self):
        return True


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

图片.kv

#:kivy 1.0
#:import kivy kivy
#:import win kivy.core.window

FloatLayout:
    canvas:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            source: 'data/images/background.jpg'
            size: self.size

    BoxLayout:
        padding: 10
        spacing: 10
        size_hint: 1, None
        pos_hint: {'top': 1}
        height: 44
        Image:
            size_hint: None, None
            size: 24, 24
            source: 'data/logo/kivy-icon-24.png'
        Label:
            height: 24
            text_size: self.width, None
            color: (1, 1, 1, .8)
            text: 'Kivy %s - Pictures' % kivy.__version__



<Picture>:
    # each time a picture is created, the image can delay the loading
    # as soon as the image is loaded, ensure that the center is changed
    # to the center of the screen.
    on_size: self.center = win.Window.center
    size: image.size
    size_hint: None, None

    Image:
        id: image
        source: root.source

        # create initial image to be 400 pixels width
        size: 400, 400 / self.image_ratio

        # add shadow background
        canvas.before:
            Color:
                rgba: 1,1,1,1
            BorderImage:
                source: 'shadow32.png'
                border: (36,36,36,36)
                size:(self.width+72, self.height+72)
                pos: (-36,-36)

我希望能够在 FloatLayout 或 Canvas 中放置一个大小标签以调整窗口大小,但它似乎不起作用。

在应用程序运行之前,我如何确定画布(或包含窗口——也许我在搜索错误的术语)的大小?

-编辑- 我发现在 .py 的包含部分中添加以下内容可以:

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

有没有办法只使用 .kv 文件来做到这一点?

【问题讨论】:

    标签: kivy


    【解决方案1】:

    不,不能以 kv 为单位设置大小。您可以像上面那样使用Config 设置它,或者您可以稍后通过设置Window 对象的大小来更改窗口大小:

    from kivy.core.window import Window
    Window.size = (640, 1163)
    

    【讨论】:

    • Ryan,这对我来说只是“有时”,我从我的应用程序构建中返回一个 Label(),有时 window.size 会发生变化,有时会发生变化,但它会返回默认值。你怎么了?
    • 有没有办法从类中访问窗口大小?
    【解决方案2】:

    要管理窗口配置,您可以在源文件顶部使用 import from kivy.config import Config

    from kivy.config import Config
    Config.set('graphics', 'resizable', '0')
    Config.set('graphics', 'width', '640')
    Config.set('graphics', 'height', '480')
    

    【讨论】:

    • 这对我不起作用,什么也没发生。应该是什么?
    【解决方案3】:

    不要使用

    from kivy.core.window import Window
    

    这使得这些不起作用

    Config.set('graphics', 'width', '200')
    Config.set('graphics', 'height', '400')
    

    【讨论】:

    • 不要问我为什么,但如果你在导入 Window 之前设置了 Config,它就可以工作。例如:from kivy.config import Config Config.set('graphics', 'width', '500') Config.set('graphics', 'height', '120') Config.set('graphics', 'fullscreen', '0') from kivy.core.window import Window 我从documentation 得到了一个提示,上面写着Config.set should be used before importing any other Kivy modules.
    猜你喜欢
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2017-06-04
    相关资源
    最近更新 更多