簡體   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