Я пытаюсь создать кнопку, которая переходит к следующему экрану, когда все счетчики имеют выбранные значения. Чтобы определить, когда счетчики имеют значения (дневные, часовые и минутные счетчики), я назначил значения для state
для каждого счетчика. Большая часть кода работает, как и ожидалось, за исключением оператора if в коде kv, который я показал ниже.
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()
if day.state == hours.state == minutes.state == AmPm.state == 'True': \
root.manager.current = 'screen_three'
Эта часть кода, по-видимому, не выполняется по какой-то причине. Переход к экрану 3 не происходит.
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)
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)
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)
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)
state: 'True'
Вот весь мой код. Если это поможет решить мою ошибку.
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.config import Config
from kivy.animation import Animation
from kivy.clock import Clock
from datetime import datetime
from datetime import timedelta
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.base import runTouchApp
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.base import runTouchApp
from kivy.uix.spinner import Spinner
# You can create your kv code in the Python file
Builder.load_string("""
<ScreenOne>:
FloatLayout:
orientation: 'horizontal'
canvas:
Rectangle:
source: 'back1.jpg'
size: self.size
pos: self.pos
BoxLayout:
Button:
background_color: 0, 0, 0, 0
on_press:
# You can define the duration of the change
# and the direction of the slide
root.manager.transition.direction = 'up'
root.manager.transition.duration = 1
root.manager.current = 'screen_two'
BoxLayout:
Label:
id: blinky
text: "Click Anywhere To Continue"
font_size: '20sp'
font_name: "Raleway-Regular"
size_hint: (1.0, 0)
alpha: 1
color: (1, 1, 1, self.alpha)
<ScreenTwo>:
FloatLayout:
orientation: 'horizontal'
canvas:
Rectangle:
source: 'back2.jpg'
size: self.size
pos: self.pos
Label:
id: white_box
size: (500,500)
alpha: 1
bcolor: (1, 1, 1, self.alpha)
Button:
background_color: 0, 0, 0, 1
size: (400, 130)
size_hint: (None, None)
pos_hint: {'right': 0.6, 'center_y': 0.30}
on_press:
root.time_now()
root.manager.current = 'screen_three'
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()
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)
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)
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)
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)
self.state: 'True'
<ScreenThree>:
BoxLayout:
Button:
background_color: 1, 0, 0, 1
on_press:
# You can define the duration of the change
# and the direction of the slide
root.manager.current = 'screen_two'
""")
# Create a class for all screens in which you can include
# helpful methods specific to that screen
Config.set('graphics', 'resizable', '0') # 0 being off 1 being on as in true/false
Config.set('graphics', 'width', '960')
Config.set('graphics', 'height', '720')
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
def __init__(self, a=1):
super(ScreenTwo, self).__init__()
self.state = state
def time_now(self):
global now, month_now, date_now, day_now, hour_now, minute_now, time_now
now = datetime.now()
print(now)
month_now = int(now.strftime("%m")) # month in int
print("month:", month_now)
date_now = int(now.strftime("%d")) # date in int
print("date:", date_now)
day_now = now.strftime("%A") # day in str
print("day of the week:", day_now)
hour_now = int(now.strftime("%H")) * 100
minute_now = int(now.strftime("%M"))
time_now = hour_now + minute_now # day in str in 24 hour format
print("time:", time_now)
def check_open(day_now, opening_days, time_now, opening_time):
global isopen
opening_days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Saturday"]
opening_time = [800, 2200]
isopen = False
if day_now in opening_days and time_now >= opening_time[0] and time_now <= opening_time[1]:
isopen = True
print("Is the store open? ", isopen)
return isopen
else:
print("Is the store open? ", isopen)
return
def on_day_select(self, text):
global day
day = str(text)
def on_hours_select(self, text):
global hours
hours = int(text)
def on_minutes_select(self, text):
global minutes
minutes = int(text)
def on_AmPm_select(self,text):
global AmPm
AmPm = str(text)
def hours_checking(self):
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
class ScreenThree(Screen):
pass
# The ScreenManager controls moving between screens
screen_manager = ScreenManager()
# Add the screens to the manager and then supply a name
# that is used to switch screens
screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))
screen_manager.add_widget(ScreenThree(name="screen_three"))
class KivyTut2App(App):
def blink_animation(self, dt):
anim = Animation(alpha=0, duration=1) + Animation(alpha=1, duration=1)
anim.repeat = True
anim.start(screen_manager.get_screen('screen_one').ids.blinky)
def build(self):
Clock.schedule_once(self.blink_animation)
return screen_manager
sample_app = KivyTut2App()
sample_app.run()