Kivy:调整大小按钮以适合下拉菜单中的换行文本 [英] Kivy: Sizing Buttons to fit wrapped text within dropdown

查看:131
本文介绍了Kivy:调整大小按钮以适合下拉菜单中的换行文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在构建Kivy下拉菜单并为文本启用自动换行时遇到问题,因此按钮小部件的大小相应地可以容纳全文.

I'm having issues building a Kivy dropdown with word wrapping enabled for the text, such that the button widgets size accordingly to accommodate the full text.

我遵循了下面的堆栈溢出线程以及也链接的类似博客文章的指导.

I have followed guidance from the stack overflow thread below and similar blog post that is also linked.

https://blog.kivy .org/2014/07/wrapping-text-in-kivys-label/

文本将按预期方式换行,但是随着文本字符串变长,呈现按钮时,文本上方和下方的填充"数量将增加.我不确定是什么原因造成的,并想消除这种影响.

The text wraps as expected, however as the text string gets longer, there is an increasing amount of "padding" above and below the text when the button is rendered. I am unsure of what is causing this and would like to eliminate this effect.

已更新:(为了使问题更简洁,已编辑代码.要匹配的已编辑图像)

Updated: (Edited code to be more concise to the issue. Edited image to match)

额外填充"与文本的长度无关,而是与下拉列表中添加的循环索引和/或小部件计数有关.

The "extra padding" is not related to the length of text, but rather the index of the loop and/or widget count added to the dropdown.

进一步编辑以下代码行:

Further editing this line of code:

btn2 = WrapButton(text=('|' + str('long text..is long...%d' % (21-index) * index) + '|') , size_hint=(1,None))

收件人:

btn2 = WrapButton(text=('|' + str('long text..is long...%d' % (21-index) * index) + '|') , size_hint=(None,None),width=700)

(设置size_hint =(none,none)而不是(1,none)并添加width = 700)

(set size_hint=(none,none) rather than (1,none) and add width=700)

消除此问题.我无法解决导致此问题的原因.编辑后的代码将失去按钮的自动宽度大小,而且我无法想象width_hint的宽度是如何引起垂直填充"的.

Eliminates the issue. I can not wrap my head around what is causing this behavior. The edited code loses the automatic width sizing for the button, and I can't imagine how a width size_hint is causing vertical "padding".

显示问题的屏幕截图

此代码演示了该问题:

from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.boxlayout import BoxLayout
from kivy.base import runTouchApp
from kivy.lang import Builder

Builder.load_string('''
<WrapButton>:
    halign: "center"
    valign: "center"
    font_size: 20
    size_hint_y: None
    text_size : self.size
    height: self.texture_size[1]
''')

class WrapButton(Button):
    pass

dropdown2 = DropDown()

layout = BoxLayout(padding=0,orientation='vertical')

mainbutton2 = WrapButton(text='Select...', size_hint=(1, None),height=95,pos_hint={'center_x': .5, 'center_y': 0})
mainbutton2.bind(on_release=dropdown2.open)
layout.add_widget(mainbutton2)

for index in range(20):
    btn2 = WrapButton(text=('|' + str('long text..is long...%d' % (21-index) * index) + '|') , size_hint=(1,None))
    btn2.bind(on_release=lambda btn2: dropdown2.select(btn2.text))
    dropdown2.add_widget(btn2)

dropdown2.bind(on_select=lambda instance, x: setattr(mainbutton2, 'text', x))

runTouchApp(layout)

屏幕截图

更新: 下面接受的答案导致Android上出现伪像.我正在测试其他设备,以排除设备本身.来自社区的任何意见将不胜感激!

Update: The answer accepted below is resulting in artifacts on Android. I am working to test on other devices to rule out the device itself. Any input from the community here would be appreciated!

工件..损坏的Kivy安装?

推荐答案

解决方案是将text_size : self.size替换为text_size : self.width, None.请参考示例和输出以获取详细信息.

The solution is to replace text_size : self.size with text_size : self.width, None. Please refer to the example and output for details.

from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.boxlayout import BoxLayout
from kivy.base import runTouchApp
from kivy.lang import Builder

Builder.load_string('''
<WrapButton>:
    halign: "center"
    valign: "center"
    font_size: 20
    size_hint_y: None
    text_size : self.width, None
    height: self.texture_size[1]
''')


class WrapButton(Button):
    pass


dropdown2 = DropDown()

layout = BoxLayout(padding=0, orientation='vertical')

mainbutton2 = WrapButton(text='Select...', size_hint=(1, None), height=95, pos_hint={'center_x': .5, 'center_y': 0})
mainbutton2.bind(on_release=dropdown2.open)
layout.add_widget(mainbutton2)

for index in range(20):
    btn2 = WrapButton(text=('|' + str('long text..is long...%d' % (21-index) * index) + '|'), size_hint=(1, None))
    btn2.bind(on_release=lambda btn2: dropdown2.select(btn2.text))
    dropdown2.add_widget(btn2)

dropdown2.bind(on_select=lambda instance, x: setattr(mainbutton2, 'text', x))

runTouchApp(layout)

输出

这篇关于Kivy:调整大小按钮以适合下拉菜单中的换行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆