【发布时间】:2019-12-14 10:25:21
【问题描述】:
在 GridLayout 内部,奇数 Button 子级不会出现。
我已经尝试了 Button 和 GridLayout 大小、size_hint、高度等的几种配置,但似乎无法解决这个问题。从子项中删除 Button 类可以解决此问题,但我想要按钮小部件的功能。
main.py:
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.gridlayout import GridLayout
from kivy.properties import NumericProperty, StringProperty, ListProperty
from kivy.uix.behaviors.focus import FocusBehavior
from kivy.uix.button import Button
class Tube(Button, RelativeLayout, FocusBehavior):
pass
class TubeMapView(GridLayout, FocusBehavior):
orderNumber = NumericProperty()
customerName = StringProperty("")
tubeList = ListProperty([])
bundleList = ListProperty([])
def add_tube(self):
self.tubeList.append(Tube())
self.add_widget(self.tubeList[-1])
def _on_focusable(self, instance, value):
self.add_tube()
def keyboard_on_key_down(self, window, keycode, text, modifiers):
print(keycode)
if keycode[1] is 'enter':
self.add_tube()
class LengthView(GridLayout):
pass
class AppView(GridLayout):
pass
class TubeMapApp(App):
pass
if __name__ == '__main__':
TubeMapApp().run()
tubemap.kv:
<Tube>:
size_hint_y: None
height: dp(60)
canvas.before:
Color:
rgba: (0,1,0,1)
Rectangle:
size: self.size
<LengthView>:
size_hint_x: 1
size_hint_y: 1
canvas.before:
Color:
rgba: (0,0,1,1)
Rectangle:
size: self.size
<TubeMapView>:
cols: 1
rows: None
size_hint_max_x:
size_hint_y: None
height: self.minimum_height
canvas.before:
Color:
rgba: (0,1,0,1)
Rectangle:
pos: self.pos
size: self.size
AppView:
cols: 2
rows: None
RelativeLayout:
size_hint_x: 0.75
ScrollView:
size: self.size
TubeMapView:
focus: True
Tube:
Tube:
Tube:
RelativeLayout:
size_hint_x: 0.25
ScrollView:
LengthView:
我希望每个 Button 都会渲染,但只有其他 Button 才会渲染,从第一个开始:
【问题讨论】:
-
你真的需要你的
Tube类来扩展RelativeLayout吗? -
不,但是随着我将更多标签和其他元素附加到 Tube 类,RelativeLayout 将更容易管理,并且将来会减少混乱。关于为什么 RelativeLayout 产生这种行为的任何想法?