如何修改我的 .kv 文件以更新显示的 image/widget 而不是将另一个 image/widget 添加到我的 kivy 应用程序中?
How can I modify my .kv file to update the shown image/widget instead of adding another image/widget onto my kivy app?
我目前遇到的问题是,我想在单击某个从滑块接收输入的按钮后向我的 kivy 应用程序添加图像(因此基本上按钮会为给定的滑块值添加不同的图像)。我现在遇到的问题是,我当前的解决方案使用 add_widget 命令,这显然会导致每次单击按钮时都会添加一个新的小部件。相反,我想要的是,更新现有的小部件而不是添加新的小部件。
test.kv:
#: import Factory kivy.factory.Factory
<TestImage1@Image>:
source: 'test_1.jpg'
#allow_stretch: True
#keep_ratio: True
pos_hint: {'centre_X':0.7}
<TestImage2@Image>:
source: 'test_2.jpg'
#allow_stretch: True
#keep_ratio: True
pos_hint: {'centre_X':0.7}
<TestBoxLayout>:
orientation:'vertical'
size: root.width,root.height
font_size: 20
padding: 100
spacing: 10
Slider:
id: slider
min: 1
max: 2
step: 1
Button:
text: 'press me'
on_press:
if slider.value == 1: root.add_widget(Factory.TestImage1())
elif slider.value == 2: root.add_widget(Factory.TestImage2())
on_release: print("ahhh")
on_state:
#print("my current state is {}".format(self.state))
size_hint: (0.3,0.3)
test.py:
import kivy
kivy.require('2.0.0')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class TestBoxLayout(BoxLayout):
pass
class TestApp(App):
def build(self):
return TestBoxLayout()
if __name__ == '__main__':
TestApp().run()
编辑:这是我之前问题的解决方案的扩展:
<TestBoxLayout>:
orientation:'vertical'
size: root.width,root.height
font_size: 20
padding: 100
spacing: 10
Slider:
id: slider
min: 1
max: 2
step: 1
Button:
text: 'press me'
on_press: img.source = 'test_1.jpg' if slider.value == 1 else 'test_2.jpg'
size_hint: (0.3,0.3)
Image:
id: img
color: (0, 0, 0, 0) if self.source is None else (1, 1, 1, 1)
pos_hint: {'centre_X':0.7}
我目前遇到的问题是,我想在单击某个从滑块接收输入的按钮后向我的 kivy 应用程序添加图像(因此基本上按钮会为给定的滑块值添加不同的图像)。我现在遇到的问题是,我当前的解决方案使用 add_widget 命令,这显然会导致每次单击按钮时都会添加一个新的小部件。相反,我想要的是,更新现有的小部件而不是添加新的小部件。
test.kv:
#: import Factory kivy.factory.Factory
<TestImage1@Image>:
source: 'test_1.jpg'
#allow_stretch: True
#keep_ratio: True
pos_hint: {'centre_X':0.7}
<TestImage2@Image>:
source: 'test_2.jpg'
#allow_stretch: True
#keep_ratio: True
pos_hint: {'centre_X':0.7}
<TestBoxLayout>:
orientation:'vertical'
size: root.width,root.height
font_size: 20
padding: 100
spacing: 10
Slider:
id: slider
min: 1
max: 2
step: 1
Button:
text: 'press me'
on_press:
if slider.value == 1: root.add_widget(Factory.TestImage1())
elif slider.value == 2: root.add_widget(Factory.TestImage2())
on_release: print("ahhh")
on_state:
#print("my current state is {}".format(self.state))
size_hint: (0.3,0.3)
test.py:
import kivy
kivy.require('2.0.0')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class TestBoxLayout(BoxLayout):
pass
class TestApp(App):
def build(self):
return TestBoxLayout()
if __name__ == '__main__':
TestApp().run()
编辑:这是我之前问题的解决方案的扩展:
<TestBoxLayout>:
orientation:'vertical'
size: root.width,root.height
font_size: 20
padding: 100
spacing: 10
Slider:
id: slider
min: 1
max: 2
step: 1
Button:
text: 'press me'
on_press: img.source = 'test_1.jpg' if slider.value == 1 else 'test_2.jpg'
size_hint: (0.3,0.3)
Image:
id: img
color: (0, 0, 0, 0) if self.source is None else (1, 1, 1, 1)
pos_hint: {'centre_X':0.7}