gpt4 book ai didi

python - 同时运行 kivy 和无限循环

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

我需要做一个倒计时,然后在手机上使用。听说kivy做手机图形不错,就尝试用这个。我正在尝试同时运行将倒计时的无限 while 循环和将创建窗口并显示剩余时间的 kivy 应用程序。我也在使用 pygame 时钟来计时。但是当我运行这段代码时,while 循环正在计算时间,但它只会创建白色的空白窗口,一段时间后会创建另一个窗口。我做错了什么?

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.core.window import Window
from multiprocessing import Process
import pygame


class MyFloat(FloatLayout):
def __init__(self, **kwargs):
super(MyFloat, self).__init__(**kwargs)

self.background = Image(source="textures/background.jpg", allow_stretch=True, keep_ratio=False)
self.add_widget(self.background)


class MyApp(App):
def build(self):
return MyFloat()


def check_time():
global days
global hours
global minutes
global seconds
global miliSeconds

miliSeconds -= 1
if miliSeconds < 0:
miliSeconds += 100
seconds -= 1
if seconds < 0:
seconds += 60
minutes -= 1
if minutes < 0:
minutes += 60
hours -= 1
if hours < 0:
hours += 24
days -= 1
if days < 0:
global active
active = False
print("End")

print("days: ", days)
print("hours: ", hours)
print("minutes: ", minutes)
print("seconds: ", seconds)
print("miliseconds: ", miliSeconds)


def loop():
while active:
check_time()

clock.tick(100)


clock = pygame.time.Clock()

days = 0
hours = 0
minutes = 0
seconds = 30
miliSeconds = 0
active = True


if __name__ == "__main__":
p1 = Process(target=loop)
p2 = Process(target=MyApp().run)

p1.start()
p2.start()

最佳答案

只需将代码的最后一部分更改为:

if __name__ == "__main__":
p1 = Process(target=loop)

p1.start()
MyApp().run()

关于python - 同时运行 kivy 和无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69934222/

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