【问题标题】:Add Toggle Buttons with if statement使用 if 语句添加切换按钮
【发布时间】:2021-02-04 21:45:37
【问题描述】:

根据输入(0、1、2 或 3),kv.file 或 py.file 应将一组切换按钮添加到现有布局。我尝试了很多,但似乎无法弄清楚并希望得到一些建议,因为这对我来说仍然有点新。

  • 所以当 self.reminder() = 0 时,不会显示切换按钮。
  • 当 self.reminder() = 1 时,显示一组切换按钮。
  • 当 self.reminder() = 2 时,显示两组切换按钮。
  • 当 self.reminder() = 3 时,显示三组切换按钮。
  • self.reminder() 不会超过 3 的值。

所以我得到了我的 .py 文件


import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
import csv
from kivy.core.window import Window

class WindowManager(ScreenManager):
    pass

#Backgroundcolour
Window.clearcolor = (0.67, 0.83, 0.88, 1)

class DiaryToday(Screen):
    user_id = 3
    # reads how many reminders (aka Toggle Buttons) the user wants from csv file
    # used for textlabel, this textlabel will not show if reminder == 0 (is stated in kv file)
    def reminder(self):
        with open('users.csv') as file:                     #TO DO: needs to be adjusted to global ID
            reader = csv.DictReader(file)
            for row in reader:
                if row['id'] == str(self.user_id):          #TO DO: needs to be adjusted to global ID
                    if row['mood_rem'] == 'Zero':
                        return(0)
                    else:
                        return(row['mood_rem'][0])


    # TO DO: STORES in CSV file which emoji you clicked
    def mood_value(self, mood):
        print (mood)

    #Takes care of which group of ToggleButtons is shown in the Diary Screen
    def add(self):
        if root.reminder() == 0:
            #should add or show no Toggle Buttons
        if root.reminder() == 1:
            #should add or show 1 set of Toggle Buttons
        if root.reminder() == 2:
            #should add or show 2 sets of Toggle Buttons
        if root.reminder()== 3:
            #should add or show 3 sets of Toggle Buttons

class LoopApp(App):
    def build(self):
        return DiaryToday()

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

还有我的 .kv 文件

    
<DiaryToday>:

    name: "diarytoday"
    answer: answer
    diaryinput: diaryinput
    id: test

    FloatLayout:

        Button:
            background_color: 0.1, 0.5, 0.6, 1
            pos_hint:{'center_y':0.05, 'center_x': 0.5}
            font_size: 18
            size_hint: 0.1, 0.05
            text: "Save"

        Button:
            pos_hint:{'center_y':0.95, 'center_x': 0.175}
            background_color: 0.1, 0.5, 0.6, 1
            font_size: 18
            size_hint: 0.15, 0.05
            text: "Previous day"

        Label:
            pos_hint:{'center_y':0.95, 'center_x': 0.5}
            size_hint: 0.1, 0.05
            color: 0.1, 0.5, 0.6, 1
            text: 'date'
            font_size: 22

        Label:
            pos_hint:{'center_y':0.87, 'center_x': 0.5}
            font_size: 18
            size_hint: 0.1, 0.05
            color: 0.1, 0.5, 0.6, 1
            text: 'Question'

        TextInput:
            font_size: 14
            size_hint: 0.8, 0.1
            pos_hint:{'center_y':0.78, 'center_x': 0.5}
            id: answer

        Label:
            pos_hint:{'center_y':0.67, 'center_x': 0.5}
            size_hint: 0.1, 0.05
            color: 0.1, 0.5, 0.6, 1
            font_size: 18
            text: 'My Diary'

        TextInput:
            font_size: 14
            size_hint: 0.8, 0.22
            pos_hint:{'center_y':0.51, 'center_x': 0.5}
            id: diaryinput

        Label:
            pos_hint:{'center_y':0.36, 'center_x': 0.5}
            size_hint: 0.1, 0.05
            color: 0.1, 0.5, 0.6, 1
            font_size: 18
            text: ' ' if root.reminder() == 0 else 'How are you feeling?'


        BoxLayout:
            id: ToggleButtonGroup1
            pos_hint: {'center_y':0.26, 'center_x': 0.5}
            size_hint: 0.5, 0.1
            ToggleButton:
                background_normal: 'VerysadEmoji1.png'
                background_down: 'VerysadEmoji2.png'
                group: "emojis"
                size_hint: 0.5, 1.3
                on_press: root.mood_value(1)
            ToggleButton:
                background_normal: 'SadEmoji1.png'
                background_down: 'SadEmoji2.png'
                group: "emojis"
                size_hint: 0.5, 1.3
                on_press: root.mood_value(2)
            ToggleButton:
                background_normal: 'MediumEmoji1.png'
                background_down: 'MediumEmoji2.png'
                group: "emojis"
                size_hint: 0.5, 1.3
                on_press: root.mood_value(3)
            ToggleButton:
                background_normal: 'HappyEmoji1.png'
                background_down: 'HappyEmoji2.png'
                group: "emojis"
                size_hint: 0.5, 1.3
                on_press: root.mood_value(4)
            ToggleButton:
                background_normal: 'VeryHappyEmoji1.png'
                background_down: 'VeryHappyEmoji2.png'
                group: "emojis"
                size_hint: 0.5, 1.3
                on_press: root.mood_value(5)


        Label:
            pos_hint:{'center_y':0.19, 'center_x': 0.5}
            size_hint: 0.1, 0.05
            color: 0.1, 0.5, 0.6, 1
            font_size: 18
            text: "Here will come Toggle Button Group 2"


        Label:
            pos_hint:{'center_y':0.12, 'center_x': 0.5}
            size_hint: 0.1, 0.05
            color: 0.1, 0.5, 0.6, 1
            font_size: 18
            text: "Here will come Toggle Button Group 3"

谢谢!

【问题讨论】:

    标签: python if-statement kivy togglebutton


    【解决方案1】:

    您可以定义一个类,它是您要添加的切换按钮组。像这样的:

    class SomeToggles(BoxLayout):
        group = StringProperty('')  # this will be the group for the ToggleButtons
    

    那么add()方法可以是:

    #Takes care of which group of ToggleButtons is shown in the Diary Screen
    def add(self):
        if self.reminder() == 0:
            print('0')
            #should add or show no Toggle Buttons
        if self.reminder() == 1:
            print('1')
            self.ids.toggles.add_widget(SomeToggles(group=str(self.grp_count)))
            self.grp_count += 1
            #should add or show 1 set of Toggle Buttons
        if self.reminder() == 2:
            print('2')
            #should add or show 2 sets of Toggle Buttons
        if self.reminder()== 3:
            print('3')
            #should add or show 3 sets of Toggle Buttons
    

    上面只处理self.reminder()返回1的情况,但其他情况类似。当然,grp_count(这只是我定义group属性的构造)需要添加:

    class DiaryToday(Screen):
        grp_count = 0
    

    为此,必须定义一个 id 为 toggles 的容器。这将是另一个BoxLayout,其中包含第一组切换按钮以及添加的按钮。所以修改后的kv 文件看起来像:

    <DiaryToday>:
    
        name: "diarytoday"
        answer: answer
        diaryinput: diaryinput
        id: test
    
        FloatLayout:
    
            Button:
                background_color: 0.1, 0.5, 0.6, 1
                pos_hint:{'center_y':0.05, 'center_x': 0.5}
                font_size: 18
                size_hint: 0.1, 0.05
                text: "Save"
    
            Button:
                pos_hint:{'center_y':0.95, 'center_x': 0.175}
                background_color: 0.1, 0.5, 0.6, 1
                font_size: 18
                size_hint: 0.15, 0.05
                text: "Previous day"
    
            Label:
                pos_hint:{'center_y':0.95, 'center_x': 0.5}
                size_hint: 0.1, 0.05
                color: 0.1, 0.5, 0.6, 1
                text: 'date'
                font_size: 22
    
            Label:
                pos_hint:{'center_y':0.87, 'center_x': 0.5}
                font_size: 18
                size_hint: 0.1, 0.05
                color: 0.1, 0.5, 0.6, 1
                text: 'Question'
    
            TextInput:
                font_size: 14
                size_hint: 0.8, 0.1
                pos_hint:{'center_y':0.78, 'center_x': 0.5}
                id: answer
    
            Label:
                pos_hint:{'center_y':0.67, 'center_x': 0.5}
                size_hint: 0.1, 0.05
                color: 0.1, 0.5, 0.6, 1
                font_size: 18
                text: 'My Diary'
    
            TextInput:
                font_size: 14
                size_hint: 0.8, 0.22
                pos_hint:{'center_y':0.51, 'center_x': 0.5}
                id: diaryinput
    
            Label:
                pos_hint:{'center_y':0.36, 'center_x': 0.5}
                size_hint: 0.1, 0.05
                color: 0.1, 0.5, 0.6, 1
                font_size: 18
                text: ' ' if root.reminder() == 0 else 'How are you feeling?'
    
            BoxLayout:   # this contains all the Toggle Button groups
                id: toggles
                orientation: 'vertical'
                pos_hint: {'center_y':0.26, 'center_x': 0.5}
                size_hint_y: None
                height: self.minimum_height
                
                BoxLayout:
                    id: ToggleButtonGroup1
                    pos_hint: {'center_y':0.26, 'center_x': 0.5}
                    size_hint: 0.5, None
                    height: self.minimum_height
                    ToggleButton:
                        background_normal: 'VerysadEmoji1.png'
                        background_down: 'VerysadEmoji2.png'
                        group: "emojis"
                        size_hint: 0.5, None
                        height: 40
                        on_press: root.mood_value(1)
                    ToggleButton:
                        background_normal: 'SadEmoji1.png'
                        background_down: 'SadEmoji2.png'
                        group: "emojis"
                        size_hint: 0.5, None
                        height: 40
                        on_press: root.mood_value(2)
                    ToggleButton:
                        background_normal: 'MediumEmoji1.png'
                        background_down: 'MediumEmoji2.png'
                        group: "emojis"
                        size_hint: 0.5, None
                        height: 40
                        on_press: root.mood_value(3)
                    ToggleButton:
                        background_normal: 'HappyEmoji1.png'
                        background_down: 'HappyEmoji2.png'
                        group: "emojis"
                        size_hint: 0.5, None
                        height: 40
                        on_press: root.mood_value(4)
                    ToggleButton:
                        background_normal: 'VeryHappyEmoji1.png'
                        background_down: 'VeryHappyEmoji2.png'
                        group: "emojis"
                        size_hint: 0.5, None
                        height: 40
                        on_press: root.mood_value(5)
    <SomeToggles>:
        pos_hint: {'center_y':0.26, 'center_x': 0.5}
        size_hint: 0.5, None
        height: self.minimum_height
        ToggleButton:
            group: root.group
            size_hint: 0.5, None
            height: 40
        ToggleButton:
            group: root.group
            size_hint: 0.5, None
            height: 40
        ToggleButton:
            group: root.group
            size_hint: 0.5, None
            height: 40
    

    上面的kv 还包括&lt;SomeToggles&gt; 规则,它定义了如何构建新的切换按钮组。您还需要提供一种调用self.reminder() 的机制。我刚刚将该调用添加到 mood_value() 方法中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      相关资源
      最近更新 更多