【问题标题】:Kivy Changing State of Toggle Button When Another Toggle Button Is Active当另一个切换按钮处于活动状态时,Kivy 更改切换按钮的状态
【发布时间】:2020-01-04 12:47:09
【问题描述】:

我有两个切换按钮。当一个处于“关闭”状态时,我需要另一个处于“正常”状态。我尝试制作一个 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}

切换按钮在评论“导出菜单内容”之后开始

【问题讨论】:

  • 只需使用ToggleButtongroup 属性将它们放在同一个组中,这将为您处理。
  • 完美!太感谢了!我将发布我的解决方案供其他人查看。

标签: python widget kivy conditional-statements togglebutton


【解决方案1】:
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!谢谢。

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

【讨论】:

    【解决方案2】:

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

    on_press: self.state = "down"
    

    【讨论】:

    • 这个答案似乎与问题没有直接关系,但确保始终选择一个切换按钮是一个很酷的主意。我发现在 Toggle Button Behavior 中有一个 allow_no_selection 选项,我应该为此目的而制作,但我还没有弄清楚如何使用它。
    猜你喜欢
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    相关资源
    最近更新 更多