Kivy:如何将开关默认从开/关更改为开/关或更改为是/否? [英] kivy: How to change Switch default from ON/OFF to OPEN/CLOSE or to YES/NO?
本文介绍了Kivy:如何将开关默认从开/关更改为开/关或更改为是/否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
On/Off
更改为Open/Close
或Yes/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屋!
查看全文