沮丧:GridLayout 中的奇怪标签间距
Kivy: Odd label spacing in GridLayout
我正在制作一个可以从不同网站获取文章的网络抓取工具。为此,我将文章小部件动态添加到它们各自的 GridLayouts 中。但是,某些屏幕上的文章会均匀地 spaced,而其他屏幕则 spaced 会偶尔出现。好像文章越长越少space。以下是我认为相关的代码片段(我会包括所有内容,但它有 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
当您在 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
.
尝试了几个小时,仍然找不到合理的解决方案,所以我想出了一个有点破旧的答案。我所做的只是根据文章标题的长度限制字符数。这样间距就不会那么零星了,因为所有的小部件都或多或少具有相同的高度。
我正在制作一个可以从不同网站获取文章的网络抓取工具。为此,我将文章小部件动态添加到它们各自的 GridLayouts 中。但是,某些屏幕上的文章会均匀地 spaced,而其他屏幕则 spaced 会偶尔出现。好像文章越长越少space。以下是我认为相关的代码片段(我会包括所有内容,但它有 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
当您在 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
.
尝试了几个小时,仍然找不到合理的解决方案,所以我想出了一个有点破旧的答案。我所做的只是根据文章标题的长度限制字符数。这样间距就不会那么零星了,因为所有的小部件都或多或少具有相同的高度。