Kivy应用加载时元素大小未更新
我有一个叫 on_size()
的事件,当我调整窗口大小时,它能正常工作,但在应用程序加载时却不能像预期那样正常工作(虽然它能工作,但效果不太对):
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\n123'
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
可能哪里出错了?
1 个回答
0
我在更新我班级里孩子们的大小时,依据里面的标签和按钮,我给它们添加了一个 on_size
事件:
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 文件中替换了按钮和标签。现在看起来一切都正常了。