gpt4 book ai didi

python - 仅当选择所有微调器时,如何在 python 文件部分中调用 screenmanager 以更改屏幕

转载 作者:行者123 更新时间:2023-12-01 00:23:33 25 4
gpt4 key购买 nike

我曾在这里发表过一篇文章(触发 screenmanager 从 if 条件更改 Kivy 中的屏幕),尝试以不同的方法执行相同的任务。我在那里没有得到任何解决方案,所以我正在尝试不同的方法。但是,我现在遇到了不同的问题。

我正在为用户创建日期和时间选择选项。为此,我尝试创建一个按钮,仅当所有微调器都选择了值时才移动到下一个屏幕(单击时)。

为了让程序识别何时单击旋转器(日、小时和分钟旋转器),我为状态分配了一个“True”值(d_state、m_state、h_state 和 A_state 分别表示日、分钟、小时、Am/Pm 分别)在每个微调器的 .py 代码中。

仅当所有微调器的状态均为 True 时,屏幕管理器才会从 screen_two 切换到 screen_ Three。当单击微调器(并选择一个选项)时,我分配 True。

除了 ScreenTwo 类中 switch_screen 函数中的 self.screen_manager.current = 'screen_two' 行之外,大多数代码都按预期工作。我知道这一点是因为我最初用一些要调试的打印语句替换了 self.screen_manager.current = 'screen_two' 行,并且它们工作得很好。这意味着只有 screen_manager 行不起作用。

我已经使用尽可能少的代码创建了代码的可运行版本(显示我的问题)。

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

Builder.load_string("""
<ScreenTwo>:
FloatLayout:
Button:
background_color: 1, 1, 1, 1
size: (400, 130)
size_hint: (None, None)
pos_hint: {'right': 0.6, 'center_y': 0.3}
on_press:
root.hours_checking()
root.switch_screen()
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.curr_selection: '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.curr_selection: '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.curr_selection: '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.curr_selection: 'True'
""")

class ScreenTwo(Screen):
def on_day_select(self, text): #Function assigns selected day from spinner to a variable
global day, d_status
day = str(text)
d_status = 'True'

def on_hours_select(self, text): #Function assigns selected 12-hour from spinner to a variable
global hours, h_status
hours = int(text)
h_status = 'True'

def on_minutes_select(self, text): #Function assigns selected minute from spinner to a variable
global minutes, m_status
minutes = int(text)
m_status = 'True'

def on_AmPm_select(self,text): #Function assigns selected a.m/p.m from spinner to a variable
global AmPm, A_status
AmPm = str(text)
A_status = 'True'

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('error1') #This line was used for my debugging output
else:
try: #This line is for the case when hours and AmPm are selected, but the other variables aren't
print(day, hours, minutes)
except:
print('error2') #This line is used for my debugging output

def switch_screen(self): #This function checks that all spinners have selected values
global d_status, h_status, m_status, A_status
try:
if d_status == h_status == m_status == A_status == 'True':
self.screen_manager.current = 'screen_two' #This line does not execute for some reason, and the screen does not switch even if all the spinners have values selected
else:
pass
except:
print('error3') #This line was used for my debugging output
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(ScreenTwo(name="screen_two"))
screen_manager.add_widget(ScreenThree(name="screen_three"))


class KivyTut2App(App):
def build(self):
return screen_manager


sample_app = KivyTut2App()
sample_app.run()

最佳答案

感谢您创建了一个独立的示例,这要好得多。您仍然可以通过删除一些 Spinner 来缩短它,因为它们在重要代码中都是相同的,但现在还可以。

您的代码最大的问题是您使用了裸露的异常,except:。这些捕获所有异常类型,因此您不知道它们为什么会发生,并且它们仅用于隐藏真正的问题。相反,最好使用 except SomeSpecificException: 来处理您期望遇到的特定问题,这样如果出现其他问题,您的程序仍然会崩溃 - 这就是您想要的,因为你还没有计划那件事,也没有明智的方法来继续。

如果您删除 error3 裸露,那么您的问题就会暴露出来:

 Traceback (most recent call last):
File "so06.py", line 123, in switch_screen
self.screen_manager.current = 'screen_two'
AttributeError: 'ScreenTwo' object has no attribute 'screen_manager'

您的问题是您尝试访问屏幕的 screen_manager 属性,但不存在此类属性。如果您阅读documentation ,你会发现你真正想要访问的是self.manager

其他问题:

  • 屏幕实际上仍然不会改变,因为您将 current 设置为 “screen_two”,这是当前事件屏幕的名称。
  • 不要使用'True'来表示发生了某些事情,而是使用实际的 bool 值True
  • 不要使用全局变量,出于您可以查找的所有正常原因。特别是,不要在未初始化它们的情况下使用它们,这意味着如果在这些变量存在之前单击按钮,您的代码将引发 NameError 。也许这就是为什么你添加了错误的 except: 行,但这只会让事情变得更糟。更简洁的解决方案是使用 ScreenTwo 对象的属性,您可以将其初始化为合理的值(即 False),然后稍后设置为 True

关于python - 仅当选择所有微调器时,如何在 python 文件部分中调用 screenmanager 以更改屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58789473/

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