Reputation: 99
I'm creating a day and time-selection option for the user. For that, I'm trying to create a button that only moves to the next screen (when clicked) when all the spinners have values selected.
For the program to identify when the spinners have been clicked (The day, hour and minute spinners), I have assigned a True
value for state
in the .kv code for each of the spinners. The screenmanager will shift from screen_two to screen_three only if all of the spinners have a state: True
. I assign True
when the spinner is clicked on (and an option is selected).
Most of the code works as expected, except for the if
statement in the kv code I have shown below.
I have removed most of my code and kept only the relevant snippets.
<ScreenTwo>:
FloatLayout:
Button:
background_color: 0, 0, 0, 1
size: (400, 130)
size_hint: (None, None)
pos_hint: {'right': 0.6, 'center_y': 0}
on_press:
root.hours_checking() #this function converts the 12hr time to 24hr time
The states of each of the spinners is checked (individual spinner states are identified by the IDs I've assigned).
if day.state == hours.state == minutes.state == AmPm.state == 'True': \ root.manager.current = 'screen_three'
Spinner:
id: day
size_hint: None, None
size: 100, 44
pos_hint: {'center': (.5, .5)}
text: 'Day'
values: 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'
on_text:
root.on_day_select(self.text) #this function sends back the selected day value in the spinner
self.state: 'True'
Spinner:
id: hours
size_hint: None, None
size: 100, 44
pos_hint: {'center': (.1, .5)}
text: 'Hour'
values: '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'
on_text:
root.on_hours_select(self.text) #this function sends back the selected hours value in the spinner
self.state: 'True'
Spinner:
id: minutes
size_hint: None, None
size: 100, 44
pos_hint: {'center': (.3, .5)}
text: 'Minutes'
values: '00', '15', '30', '45'
on_text:
root.on_minutes_select(self.text) #this function sends back the selected minutes value in the spinner
self.state: 'True'
Spinner:
id: AmPm
size_hint: None, None
size: 100, 44
pos_hint: {'center': (.4, .5)}
text: 'AM/PM'
values: 'a.m', 'p.m'
on_text:
root.on_AmPm_select(self.text) #this function sends back the selected day a.m/p.m value in the spinner
self.state: 'True'
class ScreenTwo(Screen):
def on_day_select(self, text): #Function assigns selected day from spinner to a variable
global day
day = str(text)
def on_hours_select(self, text): #Function assigns selected 12-hour from spinner to a variable
global hours
hours = int(text)
def on_minutes_select(self, text): #Function assigns selected minute from spinner to a variable
global minutes
minutes = int(text)
def on_AmPm_select(self,text): #Function assigns selected a.m/p.m from spinner to a variable
global AmPm
AmPm = str(text)
def hours_checking(self): #Function converts 12hr time to 24hr time
global AmPm
global hours
global minutes
global day
try:
if 1 <= hours <= 11 and AmPm == 'a.m':
pass
elif 1 <= hours <= 12 and AmPm == 'p.m':
hours += 12
elif hours == 12 and AmPm == 'a.m':
hours = 0
except:
print('error')
else:
print(day, hours, minutes)
pass
When this code is run and I click the button, I receive the 12hr time in the 24hr time format. Which means everything up till the screenmanager part in the kv file (which is shown in the code block, highlighted in the yellow part) runs perfectly. I'm very new to Kivy and OOP, so please help me see where I exactly went wrong.
Upvotes: 0
Views: 360
Reputation: 39052
You can create your own property for your purpose by creating an extension of the Spinner
like this:
class MySpinner(Spinner):
current_selection = BooleanProperty(False)
Then, replace Spinner
in your kv
with MySpinner
, and everywhere that you reference the state
of a MySpinner
, replace it with current_selection
.
Upvotes: 0