【问题标题】:Kivy: Odd label spacing in GridLayoutKivy:GridLayout 中的奇数标签间距
【发布时间】:2020-07-09 14:16:58
【问题描述】:

我正在制作一个可以从不同网站获取文章的网络爬虫。为此,我将文章小部件动态添加到它们各自的 GridLayouts 中。但是,有些屏幕上的文章是均匀分布的,而另一些则是零星分布的。似乎文章越长,空间就越少。下面是我认为相关的代码的 sn-ps(我会包括所有内容,但它是 1300 多行)。

例子:

良好的间距

间距不好

Python:

def text_collision(self):
    labels = [i for i in self.news_articles.children]
    for label in labels:
        font_instance = 35
        label.size = label.texture_size
        if label.collide_point(*label.to_widget(*Window.mouse_pos)):
            animation = Animation(font_size=font_instance + 2, s=1 / 60, duration=.06)
            label.color = (.96, .60, .61, 1)
            if label.count == 0:
                animation.start(label)
                label.count += 1
        else:
            label.count = 0
            Animation.cancel_all(label)
            label.color = (1, 1, 1, 1)
            label.font_size = font_instance

def articles(self):
    titles = self.csv_load()[0]
    links = self.csv_load()[1]
    for lnk, items in zip(links, titles):
        if len(items.strip()) == 0 or len(lnk.strip()) == 0:
            continue
        article_widget = Label(text="[ref={}][b]{}[/b][/ref]".format(lnk, items), markup=True, 
                         font_size=35, text_size=(700, None), halign='left', size_hint_y=None)
        self.news_articles.add_widget(article_widget)
        article_widget.on_ref_press = self.openlink

基维:

ScrollView:
    do_scroll_x: False
    do_scroll_y: True
    pos: (50, -140)
    smooth_scroll_end: 5

    GridLayout:
        id: articles
        cols: 1
        col_default_width: 700
        padding: [0, 140, 0, 0]
        size_hint_y: None
        height: self.minimum_height
        spacing: 150

【问题讨论】:

    标签: python python-3.x kivy python-3.6 kivy-language


    【解决方案1】:

    当您在GridLayout 中使用height: self.minimum_height 时,您必须为孩子们明确定义heights。所以size_hint_y=None 不提供height 的值会导致问题。尝试为 Aricle_Widget 创建 kv 规则,如下所示:

    class Article_Widget(Label):
        pass
    

    而在kv

    <Article_Widget>:
        markup: True
        font_size: 35
        text_size: (700, None)
        halign: 'left'
        size_hint_y: None
        height: self.texture_size[1]
    

    然后在您的articles() 方法中:

    article_widget = Article_Widget(text="[ref={}][b]{}[/b][/ref]".format(lnk, items))
    

    我没有测试过这段代码,所以我不能保证它会起作用。但想法是让Label 确定其height,以便GridLayout 可以正确计算self.minimum_height

    【讨论】:

    • 感谢您的帮助!虽然这个特定的代码不能完美运行,但我现在完全可以调整它,因为我确实了解了问题所在。
    【解决方案2】:

    尝试了几个小时,仍然找不到合理的解决方案,所以我想出了一个有点磨损的答案。我所做的只是根据文章标题的长度限制字符数量。这样间距就不那么分散了,因为所有小部件的高度都差不多。

    【讨论】:

      猜你喜欢
      • 2012-03-28
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多