如何使用Python Kivy中的图像刷新窗口



当我点击按钮时,我想看到两个主图像之间的三个图像,但程序只等待3秒钟并刷新主图像,我看不到其他三个图像。def rotate_room函数中的其他图像,但窗口不会加载它们。

import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.config import Config
from time import sleep
kivy.require('1.11.1')
Config.set('graphics', 'resizable', True)
Config.set('graphics', 'width', '1280')
Config.set('graphics', 'height', '720')
class Exhibition(App):
def __init__(self):
super().__init__()
self.img = Image(source="img/01.jpg", id="img")
self.counter = 1
self.f1 = FloatLayout()
def build(self):    
b_left = Button(text="<", id="b_left", size_hint=(0.15, 1), pos_hint={"left": 1},
background_color=(10, 10, 10, 0.1))
b_right = Button(text=">", id="b_right", size_hint=(0.15, 1), pos_hint={"right": 1},
background_color=(10, 10, 10, 0.1))
self.f1.add_widget(self.img)
self.f1.add_widget(b_left)
self.f1.add_widget(b_right)
b_left.bind(on_press=self.change_img)
Clock.
b_right.bind(on_press=self.change_img)
return self.f1
def change_img(self, instance):
if instance.id == "b_left":
self.counter = self.counter + 1
if self.counter > 4:
self.counter = 1
self.rotate_room()
self.img.source = 'img/0' + str(self.counter) + '.jpg'
if instance.id == "b_right":
self.counter = self.counter - 1
if self.counter < 1:
self.counter = 4
self.rotate_room()
self.img.source = 'img/0' + str(self.counter) + '.jpg'
def rotate_room(self):
for i in range(1, 4):
self.img.source = 'img/rot/01-' + str(i) + '.jpg'
sleep(1)
if __name__ == "__main__":
app = Exhibition()
app.run()
正如文档所说,time.sleep的函数是";将执行延迟给定的秒数";。这不是你想要的,因为在gui中绘制你的更改是程序执行的一部分,你特别不想延迟。

不要使用带有睡眠的循环,而是使用Clock.schedule_interval和/或Clock.schedule_once在程序中安排更新,而不会实际阻止它

最新更新