繁体   English   中英

如何在 Python、Kivy 中按下按钮时更改按钮的背景颜色

[英]How to change the background colour of a button when it is pressed in 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

            

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM