gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:56:48 25 4
gpt4 key购买 nike

我正在尝试创建一个按钮,当所有微调器都选择了值时,该按钮会移动到下一个屏幕。为了确定旋转器何时具有值(日、小时和分钟旋转器),我为每个旋转器分配了 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。

        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()

最佳答案

on_text:
root.on_day_select(self.text)
state: 'True'

state: True这一部分没有做任何有用的事情。我假设您使用的是 Python 3,因此它被解析为 annotation .

您可能想使用self.state = True 。除非您定义了state,否则这仍然不起作用。类上的属性(您可能没有),也因为您的 if day.state == hours.state == minutes.state == AmPm.state == 1正在检查值 1,而不是值 'True'这是一个字符串。

关于python - 在 Kivy 中通过 if 条件更改屏幕似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58778611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com