<bdo id='jgsvC'></bdo><ul id='jgsvC'></ul>

      <tfoot id='jgsvC'></tfoot>

    1. <legend id='jgsvC'><style id='jgsvC'><dir id='jgsvC'><q id='jgsvC'></q></dir></style></legend>
    2. <small id='jgsvC'></small><noframes id='jgsvC'>

    3. <i id='jgsvC'><tr id='jgsvC'><dt id='jgsvC'><q id='jgsvC'><span id='jgsvC'><b id='jgsvC'><form id='jgsvC'><ins id='jgsvC'></ins><ul id='jgsvC'></ul><sub id='jgsvC'></sub></form><legend id='jgsvC'></legend><bdo id='jgsvC'><pre id='jgsvC'><center id='jgsvC'></center></pre></bdo></b><th id='jgsvC'></th></span></q></dt></tr></i><div id='jgsvC'><tfoot id='jgsvC'></tfoot><dl id='jgsvC'><fieldset id='jgsvC'></fieldset></dl></div>

    4. 在 Kivy 中加载应用程序时元素的大小不会更新

      时间:2023-06-06

          <bdo id='ARprj'></bdo><ul id='ARprj'></ul>
              <legend id='ARprj'><style id='ARprj'><dir id='ARprj'><q id='ARprj'></q></dir></style></legend>

              <small id='ARprj'></small><noframes id='ARprj'>

              <i id='ARprj'><tr id='ARprj'><dt id='ARprj'><q id='ARprj'><span id='ARprj'><b id='ARprj'><form id='ARprj'><ins id='ARprj'></ins><ul id='ARprj'></ul><sub id='ARprj'></sub></form><legend id='ARprj'></legend><bdo id='ARprj'><pre id='ARprj'><center id='ARprj'></center></pre></bdo></b><th id='ARprj'></th></span></q></dt></tr></i><div id='ARprj'><tfoot id='ARprj'></tfoot><dl id='ARprj'><fieldset id='ARprj'></fieldset></dl></div>

              <tfoot id='ARprj'></tfoot>
                  <tbody id='ARprj'></tbody>

              1. 本文介绍了在 Kivy 中加载应用程序时元素的大小不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有 on_size() 事件,它在我调整窗口大小时有效,但我无法使其在应用程序加载时正常工作(它有效,但不是必须的):

                I have on_size() event, it works when I resize window, but I can't make it work on application loading correctly (it works, but not like it must):

                from random import random, randint
                
                import kivy
                
                kivy.require('1.8.0')
                
                from kivy.config import Config
                
                Config.set('graphics', 'fullscreen', '0')
                Config.set('graphics', 'width', 640)
                Config.set('graphics', 'height', 480)
                Config.set('graphics', 'position', 'custom')
                Config.set('graphics', 'top', 40)
                Config.set('graphics', 'left', 40)
                
                from kivy.app import App
                from kivy.uix.layout import Layout
                from kivy.uix.boxlayout import BoxLayout
                from kivy.uix.label import Label
                from kivy.graphics import Color, Line, Rectangle
                
                
                class Main(BoxLayout):
                    side_padding = 20
                
                    def __init__(self, **kwargs):
                        super(Main, self).__init__(**kwargs)
                        self.size = (0, 0)  # Doesn't help.
                        self.on_size()  # Doesn't help.
                
                    def on_size(self, *args):
                        left, middle, right = self.ids.left, self.ids.middle, self.ids.right
                        left.width = max([x.texture_size[0] for x in left.children]) + self.side_padding
                        right.width = max([x.texture_size[0] for x in right.children]) + self.side_padding
                        available_space = self.width - left.width - right.width
                        if available_space > self.height:
                            middle.size = (self.height, self.height)
                            extra = (self.width - self.height - left.width - right.width) // 2
                            left.width += extra
                            right.width += extra
                        else:
                            middle.size = (available_space, available_space)
                
                
                class SidePanel(BoxLayout):
                    def __init__(self, **kwargs):
                        super(SidePanel, self).__init__(**kwargs)
                
                
                class GameField(Layout):
                    def __init__(self, **kwargs):
                        super(GameField, self).__init__(**kwargs)
                        self.array_size = randint(4, 8)
                        self.bind(pos=self.update, size=self.update)
                        self.update()
                
                    def update(self, *args):
                        s_padding = 1 / 16
                        s_sq_size = 1 - 2 * s_padding
                        self.canvas.clear()
                        with self.canvas:
                            block_size = self.width / self.array_size
                            sq_size = block_size * s_sq_size
                            padding = block_size * s_padding
                            Color(1, 1, 1)
                            for j in range(self.array_size):
                                for i in range(self.array_size):
                                    Rectangle(pos=(self.x + padding + i * block_size, self.y + padding + j * block_size),
                                              size=(sq_size, sq_size))
                            for i in range(self.array_size + 1):
                                shift = self.width * i / self.array_size
                                Line(points=(self.x, self.y + shift, self.x + self.width, self.y + shift))
                                Line(points=(self.x + shift, self.y, self.x + shift, self.y + self.height))
                
                
                    def on_touch_up(self, touch):
                        self.array_size = randint(4, 8)
                        self.update()
                
                
                class Application(App):
                    def build(self):
                        return Main()
                
                
                if __name__ == '__main__':
                    Application().run()
                

                *.kv:

                #:kivy 1.8.0
                
                <Main>:
                    orientation: 'horizontal'
                    canvas:
                        Color:
                            rgb: 1, 1, .9
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    SidePanel:
                        id: left
                        Label:
                            size_hint_y: None
                            color: 0, 0, 0, 1
                            text: 'Score'
                        Label:
                            color: 0, 0, 0, 1
                            text: 'Event log log log loooooooooog'
                        Label:
                            color: 0, 0, 0, 1
                            text: 'Event log log log
                123'
                        Label:
                            color: 0, 0, 0, 1
                            text: '35mdbj65 qewr'
                        Label:
                            color: 0, 0, 0, 1
                            text: '3qht6ju7ustju'
                    GameField:
                        id: middle
                    SidePanel:
                        id: right
                        Button:
                            size_hint_y: None
                            text: 'Menu'
                        Label:
                            color: 0, 0, 0, 1
                            text: 'Bonuses bonuses'
                <SidePanel>:
                    orientation: 'vertical'
                    size_hint_x: None
                    size: self.size
                
                <GameField>:
                    pos_hint: {'center_y': .5}
                    size_hint: None, None
                    canvas.before:
                        Color:
                            rgb: .9, .9, .9
                        Rectangle:
                            pos: self.pos
                            size: self.size
                

                也许有什么错误?

                推荐答案

                当我根据里面的标签和按钮更新班级孩子的大小时,我已经向他们添加了 on_size 事件:

                As I update my class's children's sizes depending on labels and buttons inside of it, I've added to them on_size event:

                def on_size(self, *args):
                    self.parent.parent.on_size()
                
                
                class SideLabel(Label):
                    on_size = on_size
                
                
                class SideButton(Button):
                    on_size = on_size
                

                我还替换了 kv 中这些类的按钮和标签.它现在似乎可以正常工作了.

                And also I have replaced Buttons and Labels on these classes in kv. It seems to work correctly now.

                这篇关于在 Kivy 中加载应用程序时元素的大小不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Kivy Filechooser 在滚动屏幕上重叠文本 下一篇:在 Kivy 中嵌入 youtube 视频

                相关文章

                  <tfoot id='qRSv7'></tfoot>
                1. <legend id='qRSv7'><style id='qRSv7'><dir id='qRSv7'><q id='qRSv7'></q></dir></style></legend>
                    <bdo id='qRSv7'></bdo><ul id='qRSv7'></ul>
                2. <small id='qRSv7'></small><noframes id='qRSv7'>

                  <i id='qRSv7'><tr id='qRSv7'><dt id='qRSv7'><q id='qRSv7'><span id='qRSv7'><b id='qRSv7'><form id='qRSv7'><ins id='qRSv7'></ins><ul id='qRSv7'></ul><sub id='qRSv7'></sub></form><legend id='qRSv7'></legend><bdo id='qRSv7'><pre id='qRSv7'><center id='qRSv7'></center></pre></bdo></b><th id='qRSv7'></th></span></q></dt></tr></i><div id='qRSv7'><tfoot id='qRSv7'></tfoot><dl id='qRSv7'><fieldset id='qRSv7'></fieldset></dl></div>