如何在Python中更改按钮的背景颜色,Kivy



我目前正在学习Kivy,不知道如何在点击按钮时更改按钮的背景颜色。我知道默认情况下颜色会变为蓝色,但我不希望发生这种情况。我有以下代码。。。

-----Python脚本----

import kivy
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder

class WindowManager(ScreenManager):
pass
class HomeScreen(Screen):
pass
class NotesScreen(Screen):
pass
class PaintingScreen(Screen):
pass
class CalculatorScreen(Screen):
pass
class ContactsScreen(Screen):
pass
class DictionairyScreen(Screen):
pass

kv = Builder.load_file("multi_pages.kv")

class MyApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyApp().run()

----.kv文件----

WindowManager:
HomeScreen:
NotesScreen:
PaintingScreen:
CalculatorScreen:
ContactsScreen:
DictionairyScreen:

<HomeScreen>:
name: "home"
FloatLayout:
Button:
text: "Notes"
pos_hint: {"x": .325, "top": .875}
size_hint: .35, .12
font_size: 20
background_color: -1, 1, 1, 1


Button:
text: "Painting"
pos_hint: {"x": .325, "top": .725}
size_hint: .35, .12
font_size: 20
color: 1, 1, 1, 1
background_color: -1, -1, -1, 0.3

on_press:
print("Clicked")
on_release:
print("Released")
on_state:
print("my current state is {}".format(self.state))



Button:
text: "Calculator"
pos_hint: {"x": .325, "top": .575}
size_hint: .35, .12
font_size: 20
Button:
text: "Contacts"
pos_hint: {"x": .325, "top": .425}
size_hint: .35, .12
font_size: 20
Button:
text: "Dictionairy"
pos_hint: {"x": .325, "top": .275}
size_hint: .35, .12
font_size: 20

非常感谢您的帮助!感谢

对于将来读到这篇文章的人,我已经对它进行了排序,你必须用Python来完成。下面是我的代码。。。

----.py文件---

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import Screen, ScreenManager

class WindowOne(Screen):
pass
class WindowManager(Screen, ScreenManager):
pass
kv_file = Builder.load_file("style.kv")
class MyApp(App):
def build(self):
return kv_file
if __name__ == "__main__":
MyApp().run()

----.kv文件----

WindowManager:
WindowOne:
<WindowOne>:
name: "Home"
btn: btn
btn2: btn2
FloatLayout:
Button:
text: "Hello world"
size_hint: 0.2, 0.2
pos_hint: {"x": 0, "top": 0.4}
background_normal: ""
background_color: 1, .5, 1, 1
id: btn
on_press:
print("Pressed button 1")
btn.background_color = RGB = .1, -.4, 1, 1
on_release:
print("Released button 1")       
btn.background_color = RGB = 1, .5, 1, 1      


Button:
text: "Goodbye world"
size_hint: .2, .2
pos_hint: {"x": 0.4, "top": 0.4}
background_normal: ""
background_color: .3, 1, -.7, 1
id: btn2
on_press:
print("Pressed button 2")
btn2.background_color = RGB = 1, -1, -.7, 1
on_release:
print("Released button 2")
btn2.background_color = RGB = .3, 1, -.7, 1

最新更新