Python 在Kivy中通过if条件更改屏幕似乎不起作用

Python 在Kivy中通过if条件更改屏幕似乎不起作用,python,kivy,kivy-language,Python,Kivy,Kivy Language,我正在尝试创建一个按钮,当所有微调器都选择了值时,该按钮将移动到下一个屏幕。为了确定微调器何时具有值(天、小时和分钟微调器),我为每个微调器的state指定了值。除了我在下面所示的kv代码中的if语句外,大多数代码都按照预期工作 Button: background_color: 0, 0, 0, 1 size: (400, 130) size_hint: (None, None) pos_hint: {'right': 0.6, 'center_y':

我正在尝试创建一个按钮,当所有微调器都选择了值时,该按钮将移动到下一个屏幕。为了确定微调器何时具有值(天、小时和分钟微调器),我为每个微调器的
state
指定了值。除了我在下面所示的kv代码中的if语句外,大多数代码都按照预期工作

    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

这是我的全部代码。如果这有助于解决我的错误

    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()
导入kivy
kivy.require('1.11.1')
从kivy.app导入应用程序
从kivy.lang导入生成器
从kivy.uix.label导入标签
从kivy.uix.widget导入widget
从kivy.uix.screenmanager导入screenmanager,屏幕
从kivy.config导入配置
从kivy.animation导入动画
从kivy.clock导入时钟
从日期时间导入日期时间
从日期时间导入时间增量
从kivy.uix.dropdown导入下拉列表
从kivy.uix.button导入按钮
从kivy.base导入runTouchApp
从kivy.uix.behaviors导入按钮行为
从kivy.uix.scrollview导入scrollview
从kivy.uix.gridlayout导入gridlayout
从kivy.base导入runTouchApp
从kivy.uix.spinner导入微调器
#您可以在Python文件中创建kv代码
生成器。加载\u字符串(“”)
:
浮动布局:
方向:“水平”
画布:
矩形:
资料来源:“back1.jpg”
大小:self.size
pos:self.pos
盒子布局:
按钮:
背景颜色:0,0,0,0
新闻界:
#您可以定义更改的持续时间
#以及滑动的方向
root.manager.transition.direction='up'
root.manager.transition.duration=1
root.manager.current='screen\u two'
盒子布局:
标签:
id:blinky
文本:“单击任意位置继续”
字体大小:“20sp”
字体名称:“Raleway常规”
大小提示:(1.0,0)
阿尔法:1
颜色:(1,1,1,self.alpha)
:
浮动布局:
方向:“水平”
画布:
矩形:
资料来源:“back2.jpg”
大小:self.size
pos:self.pos
标签:
id:白盒子
尺寸:(500500)
阿尔法:1
b颜色:(1,1,1,self.alpha)
按钮:
背景颜色:0,0,0,1
尺寸:(400130)
大小提示:(无,无)
位置提示:{'right':0.6,'center_y':0.30}
新闻界:
root.time_now()
root.manager.current='screen\u three'
按钮:
背景颜色:0,0,0,1
尺寸:(400130)
大小提示:(无,无)
位置提示:{'right':0.6,'center_y':0}
新闻界:
root.hours\u checking()
如果day.state==hours.state==minutes.state==AmPm.state==True':\
root.manager.current='screen\u three'
微调器:
id:白天
大小提示:无,无
尺码:100,44
位置提示:{'center':(.5,.5)}
文字:“一天”
值:“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”
关于文本:
root.on\u day\u选择(self.text)
self.state:'True'
微调器:
id:小时
大小提示:无,无
尺码:100,44
位置提示:{'center':(.1,.5)}
文字:“小时”
值:“01”、“02”、“03”、“04”、“05”、“06”、“07”、“08”、“09”、“10”、“11”、“12”
关于文本:
root.on\u hours\u选择(self.text)
self.state:'True'
微调器:
id:分钟
大小提示:无,无
尺码:100,44
位置提示:{'center':(.3,.5)}
文字:“分钟”
值:“00”、“15”、“30”、“45”
关于文本:
root.on\u minutes\u select(self.text)
self.state:'True'
微调器:
id:AmPm
大小提示:无,无
尺码:100,44
位置提示:{'center':(.4.5)}
文字:“上午/下午”
价值观:“上午”、“下午”
关于文本:
root.on\u AmPm\u select(self.text)
self.state:'True'
:
盒子布局:
按钮:
背景颜色:1,0,0,1
新闻界:
#您可以定义更改的持续时间
#以及滑动的方向
root.manager.current='screen\u two'
""")
#为您可以包含的所有屏幕创建一个类
#特定于该屏幕的有用方法
Config.set('graphics'、'resizable'、'0')#0处于关闭状态,1处于打开状态,如真/假
Config.set('graphics'、'width'、'960')
Config.set('graphics'、'height'、'720')
一级屏幕(屏幕):
通过
第二类屏幕(屏幕):
定义初始化(self,a=1):
超级(屏幕二,自我)。\uuuu初始化
self.state=状态
现在定义时间(自我):
现在是全局,现在是月,现在是日期,现在是天,现在是小时,现在是分钟,现在是时间
now=datetime.now()
打印(现在)
month_now=int(now.strftime(“%m”)#以int表示的月份
打印(“月份:,现在的月份”)
date_now=int(now.strftime(“%d”)#日期为int
打印(“日期:,当前日期)
day_now=now.strftime(“%A”)#str中的day
打印(“一周中的某一天:,现在的某一天”)
hour_now=int(now.strftime(“%H”))*100
分钟\u now=int(now.strftime(“%M”))
time_now=hour_now+minute_now#day,str格式,24小时格式
打印(“时间:,现在时间”)
def check_open(当前日期、当前打开日期、当前时间、当前打开时间):
全球等参线
开放日=[“周一”、“周二”、“周三”
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()
on_text:
    root.on_day_select(self.text)
    state: 'True'