【发布时间】:2021-02-25 12:01:52
【问题描述】:
我已经找了两个小时的答案了。我想在 kivy 中完成的是:
import time
clear = "\033[2J\033[1;1f"
print("""
################################
##this should display an image##
################################""")
time.sleep(1)
print(clear)
print("""
#####################################
##this should display another image##
#####################################""")
这是我已经准备好的程序的开始,但不像 kivy 那样作为真正的桌面应用程序。
程序以显示图像开始,暂停一秒钟,然后在使用按钮等启动主菜单之前不显示图像(不重要)。
这是我目前所拥有的(不工作):
class Intro(FloatLayout):
connection = Image(source='connection.png')
noconnection = Image(source='noconnection.png')
checkloop = True
def check_connection(self, dt):
url='http://www.google.com/'
timeout=5
connected.stop()
noconnection.stop()
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
_ = requests.get(url, timeout=timeout)
connected.play()
self.add_widget(self.connection)
Clock.schedule_once(self.remove_widget(self.connection), 1)
checkloop = False
except requests.ConnectionError:
noconnection.play()
self.add_widget(self.noconnection)
self.remove_widget(self.noconnection)
txtinput = TextInput(text='ConnectionError. Enter to try again...')
except smtplib.SMTPConnectError:
noconnection.play()
img = Image(source='noconnection.png')
self.add_widget(self.noconnection)
self.remove_widget(self.noconnection)
txtinput = TextInput(text='SMTPConnectError. Enter to try again...')
class I41(App):
def build(self):
self.icon = 'riseicon.png'
self.title = '41'
intro = Intro()
Clock.schedule_once(intro.check_connection)
print("DONE")
我到底做错了什么?
我希望你们能帮助我!在 stackoverflow 上提出的第一个问题!
【问题讨论】:
-
您不能将 time.sleep(1) 与 Kivy 一起使用,因为它会阻塞并导致应用程序挂起。您必须使用 kivy.clock 设置计时器。看看这个kivy.org/doc/stable/api-kivy.clock.html。
-
您可以在 Kivy 中使用 time.sleep,它只是不能满足您的需求,而使用 Clock.schedule_once 是一个合适的解决方案。
-
另外,您的
returns 没有意义 - 该函数在遇到第一个值时会立即返回一个值,以后的任何值都将永远无法达到。