如何在Kivy中更改禁用小部件的颜色?

时间:2015-06-24 17:12:01

标签: python kivy

这只是我申请的一小部分 我有一个按钮"嗨"。
我还有2个按钮" 1"和" 2"在gridlayout和id是w_10
按下"嗨",w_10将被禁用 现在,我不想要" 1"和" 2"禁用后更改颜色,我想保持它们与过去状态相同,即绿色。请改进我的代码。

我的代码:

#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

Builder.load_string("""
<MenuScreen>:
    GridLayout:
        cols: 1
        Button:
            text: 'HI'
            on_press: root.change()
        GridLayout:
            id: w_10
            rows:1
            Button:
                text: '1'
                background_normal: ''
                background_color:(0.207, 0.635, 0.423, 0.9)
            Button:
                text:'2'
                background_normal: ''
                background_color:(0.207, 0.635, 0.423, 0.9)               
""")

# Declare both screens
class MenuScreen(Screen):
    def change(self):
        self.ids['w_10'].disabled=True

sm = ScreenManager()
menu = MenuScreen(name='menu')
sm.add_widget(menu)

class TestApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

1 个答案:

答案 0 :(得分:1)

将yout按钮的background_disabled_normal属性设置为emtpy字符串:

#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

Builder.load_string("""
<MenuScreen>:
    GridLayout:
        cols: 1
        Button:
            text: 'HI'
            on_press: root.change()
        GridLayout:
            id: w_10
            rows:1
            Button:
                background_disabled_normal: ''
                text: '1'
                background_normal: ''
                background_color:(0.207, 0.635, 0.423, 0.9)
            Button:
                background_disabled_normal: ''
                text:'2'
                background_normal: ''
                background_color:(0.207, 0.635, 0.423, 0.9)               
""")

# Declare both screens
class MenuScreen(Screen):
    def change(self):
        self.ids['w_10'].disabled = True

sm = ScreenManager()
menu = MenuScreen(name='menu')
sm.add_widget(menu)

class TestApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

如果您想要保持文本颜色不变,请同时使用disabled_color properties:

Button:
    background_disabled_normal: ''
    disabled_color: 1, 1, 1, 1
    text: '1'
    background_normal: ''
    background_color:(0.207, 0.635, 0.423, 0.9)