Kivy 在另一个切换按钮处于活动状态时更改切换按钮的状态

问题描述 投票:0回答:3

我有两个切换按钮。当一个处于“关闭”状态时,我需要另一个处于“正常”状态。我尝试制作 if 语句,但它使两个按钮同时具有相同的状态。这是:

on_state: exlexport.state = "down" if exlexport.state == "normal" else "normal"

这是我的完整代码:

<SettingsWindow>:
name:"settings"
FloatLayout:
Widget:
    canvas.before:
        # Background
        Rectangle:
            pos: self.pos
            size: self.size
            source: "Images/logo_br.png"
        # Brothers Menu
        Color:
            rgba: 1,1,1,.3
        Rectangle:
            size: 200, 500
            pos: self.width/10, self.height/7
        Color:
            rgba: 0,0,0,.5
        Rectangle:
            size: 190, 350
            pos: self.width/9.4, self.height/3
        # Jobs Menu
        Color:
            rgba: 1,1,1,.3
        Rectangle:
            size: 200, 500
            pos: self.width/2.5, self.height/7
        Color:
            rgba: 0,0,0,.5
        Rectangle:
            size: 190, 350
            pos: self.width/2.465, self.height/3
        # Export Menu
        Color:
            rgba: 1,1,1,.3
        Rectangle:
            size: 200, 250
            pos: self.width/1.43, self.height/3.08
        Color:
            rgba: 0,0,0,.5
        Rectangle:
            size: 190, 205
            pos: self.width/1.416, self.height/3
# Brothers Scroll List
ScrollView:
    size_hint: (None, None)
    size: (150, 325)
    pos_hint: {'center_x': .23, 'center_y': .62}
    # Brothers Menu Scroll Label
    Label:
        size_hint: None, None
        size: self.texture_size
        text: root.pretty_list_people
    # Jobs Menu Scroll Label
ScrollView:
    size_hint: (None, None)
    size: (150, 325)
    pos_hint: {'center_x': .53, 'center_y': .62}
    Label:
        size_hint: None, None
        size: self.texture_size
        text: root.pretty_list_jobs
Button:
    text:"Back"
    size_hint: 0.1, 0.1
    pos_hint: {"x":0, "y":0}
    background_color: 1,1,1,.6
    on_release:
        app.root.current = "main"
        root.manager.transition.direction = 'right'
# Brothers Title
Label:
    text: "Brothers"
    font_size: 30
    italic: True
    pos_hint: {"x":-0.275, "y":0.45}
    color: 0,0,0,1
# Jobs Title
Label:
    text: "Jobs"
    font_size: 30
    italic: True
    pos_hint: {"x":0.02, "y":0.45}
    color: 0,0,0,1
# Exporting Title
Label:
    text: "Exporting"
    font_size: 30
    italic: True
    pos_hint: {"x":0.325, "y":0.21}
    color: 0,0,0,1
# Brothers Menu Buttons
Button:
    text:"Update"
    size_hint: 0.25, 0.1
    pos_hint: {"x":0.1, "y":0.144}
    on_press: root.Pretty_Print_People(root.get_People())
Button:
    text:"Add"
    size_hint: 0.125, 0.09
    pos_hint: {"x":0.1, "y":0.243}
    on_press: root.showpop_addbro()
Button:
    text:"Remove"
    size_hint: 0.125, 0.09
    pos_hint: {"x":0.225, "y":0.243}
    on_press: root.showpop_removebro()
Button:
    text:"Update"
    size_hint: 0.25, 0.1
    pos_hint: {"x":0.1, "y":0.144}
    on_press: root.Pretty_Print_People(root.get_People())
# Jobs Menu Buttons
Button:
    text:"Add"
    size_hint: 0.125, 0.09
    pos_hint: {"x":0.4, "y":0.243}
    on_press: root.showpop_addjob()
Button:
    text:"Remove"
    size_hint: 0.125, 0.09
    pos_hint: {"x":0.525, "y":0.243}
    on_press: root.showpop_removejob()
Button:
    text: "Update"
    size_hint: 0.25, 0.1
    pos_hint: {"x":0.4, "y":0.144}
    on_press: root.Pretty_Print_Jobs(root.get_Jobs())
# Exporting Menu Content
ToggleButton:
    id: txtexport
    text: "Toggle Text File Export"
    size_hint: 0.236, 0.08
    pos_hint: {"x":0.707, "y":0.59}
    on_state: exlexport.state = "down" if exlexport.state == "normal" else "normal"
ToggleButton:
    id: exlexport
    text: "Toggle Excel File Export"
    size_hint: 0.236, 0.08
    pos_hint: {"x":0.707, "y":0.51}

切换按钮在注释“导出菜单内容”之后启动

python widget kivy conditional-statements togglebutton
3个回答
2
投票
ToggleButton:
    id: txtexport
    group: 'exportopts'
    text: "Toggle Text File Export"
    size_hint: 0.236, 0.08
    pos_hint: {"x":0.707, "y":0.59}
    state: 'down'
ToggleButton:
    id: exlexport
    group: 'exportopts'
    text: "Toggle Excel File Export"
    size_hint: 0.236, 0.08
    pos_hint: {"x":0.707, "y":0.51}

这个解决方案归功于 John Anderson!谢谢你。

只需通过组属性将切换按钮分配到同一组,并将其中一个按钮的状态设置为“向下”。


2
投票

我正在寻找一种方法来始终使我的一个切换按钮处于“向下”状态,因为属于同一组,请不要向我保证,它们都可以是“正常”的。为了获得该结果并始终“按下”一个按钮,我在每个按钮上添加了这一行:

on_press: self.state = "down"

0
投票
<tb@ToggleButton>
     group: 'tbgroup'
     allow_no_selection: False
     canvas.before:
        Color:
            rgba: [0,0,0,0] if self.state == 'normal' else rgba('#9dccc6')
        Rectangle:
            pos: self.pos
            size: self.size

Boxlayout:
    tb:
        text: 'tb1'
        state: 'down'
    tb:
        text: 'tb2''
            
© www.soinside.com 2019 - 2024. All rights reserved.