简体   繁体   中英

Kivy Changing State of Toggle Button When Another Toggle Button Is Active

I have two toggle buttons. When one is in the "down" state I need the other one to be in the "normal" state. I tried making an if statement but it makes both buttons have the same state at the same time. Here it is:

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

Here is my full code:

<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}

The toggle buttons start after the comment "Exporting Menu Content"

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}

Credit to this solution goes to John Anderson! Thank you.

Just assign the toggle buttons the same group via group property and set the state of one them to 'down'.

I was looking for a way to always make one of my toggle buttons be "down" because belonging to the same group, don't assure me that, they can both be "normal". In order to get that result and have always one button "down", I've added this line on each one:

on_press: self.state = "down"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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