【问题标题】:Kivy CheckBox Looks Like Solid Black Box (Not a Checkbox)Kivy CheckBox 看起来像纯黑框(不是复选框)
【发布时间】:2015-06-23 11:54:43
【问题描述】:

我正在制作一个 BoxLayout 小部件(orientation = 'horizo​​ntal'),其中包含三个小部件、一个标签、一个文本框和一个复选框。

thisRow = BoxLayout(orientation='horizontal')
l = Label(text='Enter plate 1:\n(Plate #)')
t = TextInput(text = 'this is a text box')
c = CheckBox()
thisRow.add_widget(l)
thisRow.add_widget(t)
thisRow.add_widget(c)

这会产生以下小部件 (thisRow):

勾选后...

最右边的黑框实际上是复选框,并且可以正常工作,但是用户无法知道它实际上是一个复选框。我希望中间有一个较小的空方格,如图所示here.

如何获得传统的复选框图像(较小的空方框)?或者一般来说,我怎样才能更明显地表明该框是一个复选框,而不仅仅是一个空标签?

谢谢

【问题讨论】:

  • 我认为问题在于复选框正在调整大小以填充整个框。你想要的是另一个容器,它允许复选框保持其自然大小并浮动在中间。

标签: python kivy


【解决方案1】:

这是一个非常有趣的问题,Malonge 以一种很好的方式尝试了它。现在(1.9.2-dev)CheckBox 的大小仍然是固定的,称之为背景。这是Widget 从 atlas 中获取的图像,并在状态发生变化时发生变化。因此,直到now 之前,都没有明确的方法来做到这一点。这是一个例子。很快,主人就会有CheckBox(color=[r,g,b,a]) 选项。谢谢;)

from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<CheckBoxBG>:
    Label:
    TextInput:
    CheckBox:
        canvas.before:
            Color:
                rgb: 1,0,0
            Rectangle:
                pos:self.center_x-8, self.center_y-8
                size:[16,16]
            Color:
                rgb: 0,0,0
            Rectangle:
                pos:self.center_x-7, self.center_y-7
                size:[14,14]
''')
class CheckBoxBG(BoxLayout):pass
runTouchApp(CheckBoxBG())

【讨论】:

    【解决方案2】:

    当背景颜色为黑色时,看起来较小的复选框被隐藏了。这是一个红色背景的例子。

    这并不理想,因为我确实喜欢黑色背景,但我现在可以用它运行。如果有人知道如何用黑色背景做到这一点,那就太好了。谢谢

    【讨论】:

      【解决方案3】:

      或者,要更改复选框背景,您可以使用图集中的另一张图片或创建图片然后加载它们:

      mycheckbox= CheckBox(
      background_checkbox_normal ='tlas://data/images/defaulttheme/button_disabled'
      background_checkbox_down = 'my_checkboxes_checked.png'
      )   
      

      在 Kivy 1.9.2.dev0(显然是从 1.9.0 版本开始)中,您可以更改复选框的背景图像。默认情况下,Kivy 使用来自 atlas* 的这些背景。

      background_checkbox_normal = StringProperty('atlas://data/images/defaulttheme/checkbox_off') #when the checkbox is not active.
      background_checkbox_down = StringProperty('atlas://data/images/defaulttheme/checkbox_on') # when the checkbox is active.
      background_checkbox_disabled_normal = StringProperty('atlas://data/images/defaulttheme/checkbox_disabled_off') #when the checkbox is disabled and not active.
      background_checkbox_disabled_down = StringProperty('atlas://data/images/defaulttheme/checkbox_disabled_on') #when the checkbox is disabled and active.
      

      你可以看看here的所有属性:

      *图集是一个包含多个纹理的包,可减少加载的图像数量并加快应用程序的加载速度。你已经在Python Install Folder\Lib\site-packages\kivy\data\images\defaulttheme-0.png看到了图集的预览

      【讨论】:

        猜你喜欢
        • 2012-11-26
        • 1970-01-01
        • 2019-09-18
        • 2020-06-13
        • 2017-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多