python - Kivy:GridLayout 中的按钮有间隙
问题描述
在 GridLayout 内部,不会出现奇数 Button 子项。
我已经尝试了 Button 和 GridLayout 大小、size_hint、高度等的几种配置,但似乎无法解决这个问题。从子项中删除 Button 类可以解决此问题,但我想要按钮小部件的功能。
主要.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()
管图.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 渲染,从第一个开始:
解决方案
如果您希望自己Tube
的RelativeLayout
行为类似于 a Button
,请将您的类的声明更改Tube
为:
class Tube(ButtonBehavior, FocusBehavior, RelativeLayout):
pass
请注意,文档说 Behavior 类必须Widget
在继承中的类之前。
这种改变符合我的想法。
推荐阅读
- linux - 使用 arm-poky-linux-g++ 交叉编译 boost 库
- flutter - 如何正确更改后颤应用程序启动器图标颜色
- typescript - 条件类型声明
- visual-studio - Microsoft.UI.Xaml 兼容性问题
- docker - Rancher 机器重新启动时未显示集群
- go - 我的游戏中的圆形碰撞比图像略大
- pyspark - Jupyter Notebook 在启动时显示 No pyspark kernel
- python-3.x - 使用自然语言处理从段落中提取特定类型的单词
- apache-kafka - 无法建立到节点 -1 的连接。经纪人可能不可用。(org.apache.kafka.clients.NetworkClient)
- python - 如何在文件之间共享静态类变量?