Skip to main content
 首页 » 编程设计

python之Kivy bool 对象属性不随复选框改变

2024年12月31日8over140

所以我试图从三个单选按钮中获取值,每个单选按钮都有自己的 bool 值分配给它们,即使我的按钮确实改变了它们自己的值,它们似乎并没有改变 值:属性。

这是处理它的 python 代码:

from kivy.uix.boxlayout import BoxLayout 
from kivy.app import App 
from kivy.properties import ObjectProperty 
from kivy.properties import BooleanProperty 
from kivy.graphics import * 
 
class CliClicker(BoxLayout): 
 
    blue = ObjectProperty(True) 
    red = ObjectProperty(False) 
    green = ObjectProperty(False) 
 
    def bg_change(self): 
        #Change background according value set in radio buttons 
        if self.blue: 
            print('color changed to blue') 
            with self.menuoptions.canvas: 
                Color(rgba=(.7,.7,.9,1)) 
        elif self.red: 
            print('color changed to red') 
            with self.menuoptions.canvas: 
                Color(rgba=(.9,.7,.7,1)) 
        elif self.green: 
            print('color changed to green') 
            with self.menuoptions.canvas: 
                Color(rgba=(.7,.9,.7,1)) 
 
    def show_value(self, instance, value, box): 
        self.value = box 
        print(instance, box, self.value) 
        print('blue', self.blue) 
        print('red', self.red) 
        print('green', self.green) 
 
class MainApp(App): 
 
    def build(self): 
        return CliClicker() 
 
app = MainApp() 
app.run() 

我包含了一个用于检查每个值状态的方法

这是代码的 Kivy 部分:

<OptionButton@Button>: 
    size_hint: (None,None) 
    width: 200 
    height: 40 
 
<CliClicker>: 
    orientation: "vertical" 
    id: cliclicker 
    menuoptions: menuopts 
 
    TabbedPanel: 
        do_default_tab: False 
        height: 200 
        id: tabs 
 
        TabbedPanelItem: 
            text: "Menu" 
            id: menu 
 
            FloatLayout: 
                id: menuopts 
                canvas: 
                    Color: 
                        rgba: .7,.7,.9,1 
                    Rectangle: 
                        pos: self.pos 
                        size: self.size 
 
                OptionButton: 
                    text: 'Option 1' 
                    pos_hint: {'right':.63, 'top':.9} 
 
                OptionButton: 
                    text: 'Option 1' 
                    pos_hint: {'right':.63, 'top':.8} 
 
                GridLayout: 
                    cols: 6 
                    size_hint: (None, None) 
                    pos_hint: {'top': .7, 'right':.69} 
                    height: 40 
                    width: 300 
 
                    canvas: 
                        Color: 
                            rgba: .9,.9,.9,1 
                        Rectangle: 
                            pos: self.pos 
                            size: self.size 
 
                    CheckBox: 
                        group: "bg_color" 
                        value: root.blue 
                        on_active: cliclicker.show_value(self, self.value, self.active) 
                        color: 0,0,0,1 
                    Label: 
                        text: 'blue' 
                        color: 0,0,0,1 
                    CheckBox: 
                        group: "bg_color" 
                        value: root.red 
                        on_active: cliclicker.show_value(self, self.value, self.active) 
                        color: 0,0,0,1 
                    Label: 
                        text: 'red' 
                        color: 0,0,0,1 
                    CheckBox: 
                        group: "bg_color" 
                        value: root.green 
                        on_active: cliclicker.show_value(self, self.value, self.active)   
                        color: 0,0,0,1 
                    Label: 
                        text: 'green' 
                        color: 0,0,0,1 

请您参考如下方法:

我觉得主要问题是你没看懂下面的意思

value: root.blue 

我认为你认为 root.blue 改变如果激活它改变但反之亦然,你指出的是每次你改变 root.blue 你改变复选框的状态并且显然你从未改变过root.blue 所以事件不会被更新。反之亦然:

<CliClicker>: 
    blue: checkbox_blue.active 
    # ... 
    CheckBox: 
        id: checkbox_blue 
        group: "bg_color" 

但即便如此这也是一个坏主意,考虑到每个功能都是一个接收输入并获得输出的黑盒子总是好的,在这种情况下输出是颜色所以我们创建一个继承自 GridLayout 的自定义小部件并公开属性颜色:

*.py

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
 
class CliClicker(BoxLayout): 
    pass 
 
class MainApp(App): 
    def build(self): 
        return CliClicker() 
 
if __name__ == '__main__': 
    MainApp().run()ç 

*.kv

<OptionButton@Button>: 
    size_hint: (None,None) 
    width: 200 
    height: 40 
 
<ColorSelector@GridLayout>: 
    cols: 6 
    color_sel: (.7,.7,.9,1) 
    canvas: 
        Color: 
            rgba: .9,.9,.9,1 
        Rectangle: 
            pos: self.pos 
            size: self.size 
    CheckBox: 
        group: "bg_color" 
        color: 0,0,0,1 
        active: True 
        on_active: if self.active: root.color_sel = .7,.7,.9,1 
    Label: 
        text: 'blue' 
        color: 0,0,0,1 
    CheckBox: 
        group: "bg_color" 
        color: 0,0,0,1 
        on_active: if self.active: root.color_sel = .9,.7,.7,1 
    Label: 
        text: 'red' 
        color: 0,0,0,1 
    CheckBox: 
        group: "bg_color" 
        color: 0,0,0,1 
        on_active: if self.active: root.color_sel = .7,.9,.7,1 
    Label: 
        text: 'green' 
        color: 0,0,0,1 
 
<CliClicker>: 
    orientation: "vertical" 
    id: cliclicker 
    menuoptions: menuopts 
 
    TabbedPanel: 
        do_default_tab: False 
        height: 200 
        id: tabs 
 
        TabbedPanelItem: 
            text: "Menu" 
            id: menu 
 
            FloatLayout: 
                id: menuopts 
                color_property:  (.7,.7,.9,1) 
                canvas: 
                    Color: 
                        rgba: self.color_property 
                    Rectangle: 
                        pos: self.pos 
                        size: self.size 
 
                OptionButton: 
                    text: 'Option 1' 
                    pos_hint: {'right':.63, 'top':.9} 
 
                OptionButton: 
                    text: 'Option 1' 
                    pos_hint: {'right':.63, 'top':.8} 
                ColorSelector: 
                    id: selector 
                    size_hint: (None, None) 
                    pos_hint: {'top': .7, 'right':.69} 
                    height: 40 
                    width: 300 
                    on_color_sel: menuopts.color_property = self.color_sel