问题描述
基于输入(0、1、2或3),kv.file或py.file应该将一组切换按钮添加到现有布局中。我做了很多尝试,但是似乎无法弄清楚,并希望得到一些建议,因为这对我来说仍然有点新。
- 因此,当self.reminder()= 0时,不会显示切换按钮。
- 当self.reminder()= 1时,显示一组切换按钮。
- 当self.reminder()= 2时,将显示两组切换按钮。
- 当self.reminder()= 3时,将显示三组切换按钮。
- self.reminder()不会超过3的值。
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,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,1
text: 'date'
font_size: 22
Label:
pos_hint:{'center_y':0.87,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,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,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,1
font_size: 18
text: "Here will come Toggle Button Group 2"
Label:
pos_hint:{'center_y':0.12,1
font_size: 18
text: "Here will come Toggle Button Group 3"
谢谢!
解决方法
您可以定义一个类,它是要添加的“切换按钮组”。像这样:
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,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,1
text: 'date'
font_size: 22
Label:
pos_hint:{'center_y':0.87,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,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,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
上面的kv
还包括<SomeToggles>
规则,该规则定义了如何构建新的切换按钮组。您还需要提供一种调用self.reminder()
的机制。我刚刚将该调用添加到了mood_value()
方法中。