繁体   English   中英

在python中以编程方式添加按钮时,kivy的size_hint和pos_hint无法正常工作

[英]Size_hint and pos_hint of kivy not working when adding buttons programmatically in python

我正在查询Mongolab以获取集合中文档的数量,然后我想为找到的每个文档添加一个按钮。 我将此代码放在屏幕的on_enter函数中:

def on_enter(self):
    topicconcepts = db.topicconcepts
    topics = topicconcepts.distinct("topic")

    for idx, topic in enumerate(topics):
        button = Button(text=topic, id=str(idx), buttonPos = idx, size=(self.width,20), pos_hint = {'top': 1}, color= (1,1,1,1), halign='center', seq = 'pre')
        # topicBox points to a BoxLayout
        self.topicBox.add_widget(button)

但是,布局实际上是这样的:

在此处输入图片说明

基本上,我希望按钮更像这样: 在此处输入图片说明

任何建议将不胜感激:)

如果要在GridLayout(或BoxLayout)中具有固定大小的按钮,则必须取消设置其size_hints。 例如,对于50dp的固定高度,不更改宽度,则为:

Button:
    size_hint_y: None
    height: '50dp'

在py中:

from kivy.metrics import dp
Button(size_hint_y=None, height=dp(50))

PS:请记住,如果给出过多的主题,则按钮可能会溢出屏幕。 若要更正此问题,请使用ScrollView小部件上下滚动它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM