.kv
文件时,图像按钮可以正确切换图像,这会导致以下问题。.kv
文件仅显示图像。即使调度了on_press
和on_release
事件,图像也不会切换。The main.py:
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.app import App
Builder.load_file('test1.kv')
class Test(FloatLayout):
pass
class TestApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TestApp().run()
<ImageButton@ButtonBehavior+Image>
<Test>:
FloatLayout:
ImageButton:
source: 'atlas://data/images/defaulttheme/checkbox_off'
on_press:
source: 'atlas://data/images/defaulttheme/checkbox_on'
on_release:
source: 'atlas://data/images/defaulttheme/checkbox_off'
查看下面的代码是否有效。
<ImageButton@ButtonBehavior+Image>
<MainMenu>:
FloatLayout:
ImageButton:
source: "C:\\Users\\kanna\\Pictures\\OpenCity-Main_Menu\\PNG\\Build-Icon.png"
on_press:
self.source = "C:\\Users\\kanna\\Pictures\\OpenCity-Main_Menu\\PNG\\Build-Icon_Click.png"
on_release:
self.source = "C:\\Users\\kanna\\Pictures\\OpenCity-Main_Menu\\PNG\\Build-Icon.png"
主要问题是在on_
事件的右侧,例如on_press
,on_release
等。语法必须像Python,而不是kvLang。因此,您的source: "c:/ bla-bla.."
必须为source = "c:/ bla-bla.."