Kivy:如何将开关默认从开/关更改为开/关或更改为是/否? [英] kivy: How to change Switch default from ON/OFF to OPEN/CLOSE or to YES/NO?

查看:15
本文介绍了Kivy:如何将开关默认从开/关更改为开/关或更改为是/否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更清楚地说,我希望将开关的文本从On/Off更改为Open/CloseYes/No。我不知道怎么做这件事。谢谢。

推荐答案

您可以通过更改开关中右侧矩形的背景图像来解决此问题。
您可以像小工具一样制作一个83x32像素的图标。
我在一个在线图片编辑sumo上做了一个非常难看的例子:

然后我将其保存为images/icon.jpg
如果您还想更改滑块,则滑块为43x32像素。

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.switch import Switch


class MyGui(Widget):

    def __init__(self,**kwargs):
        super(MyGui,self).__init__(**kwargs)

        self.switch = Switch()

        self.switch.canvas.children[2].source = "images/icon.jpg" # The slider is the last element, so in that case -> children[-1].source

        self.add_widget(self.switch)



class MyApp(App):

    def build(self):
        return MyGui()


if __name__ == '__main__':
    MyApp().run()

如果您想用KV语言执行此操作,您可以这样做:

from kivy.lang import Builder

Builder.load_string("""

<MyGui>:
    Switch:
        canvas:
            Color:
                rgb: 1,1,1
            Rectangle:
                source: 'switch.jpg'        # make or download your background jpg
                size: sp(83), sp(32)
                pos: int(self.center_x - sp(41)), int(self.center_y - sp(16))
            Rectangle:
                source: 'switch_slider.jpg' # make or download your slider jpg
                size: sp(43), sp(32)
                pos: int(self.center_x - sp(41) + self.active_norm_pos * sp(41)), int(self.center_y - sp(16))
""")

class MyGui(Widget):
    pass

这篇关于Kivy:如何将开关默认从开/关更改为开/关或更改为是/否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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