0

我有重叠两个线程的问题。连接 Tkinter 的最佳方法是什么,以及内部带有无限循环的方法,以免冻结整个 gui。我以两个线程和控制标志的形式实现了它。一切正常,除了我可以同时运行两个动画,这会导致不好的效果。

 def __init__(self, master):
            self.btnRainbowOn = Button(
                master, text="     Włącz     ", command=self.start_rainbow)
            self.btnRainbowOff = Button(
                master, text="Wyłącz", command=Animation.stop_animation)
            self.btnRainbowAllOn = Button(
                master, text="    Włącz    ", command=self.start_rainbow_all)
            self.btnRainbowOff2 = Button(
                master, text="Wyłącz", command=Animation.stop_animation)

    def start_rainbow(self):
        """Run rainbow animation on separate thread."""
        Animation.start_animation()
        thread = threading.Thread(
            target=Animation.rainbow_cycle, args=(strip,))
        thread.start()

    def start_rainbow_all(self):
        """Run rainbow animation on separate thread, all pixels at once."""
        Animation.start_animation()
        thread = threading.Thread(target=Animation.rainbow, args=(strip,))
        thread.start()

其他带有动画类的文件

    @classmethod
    def stop_animation(self):
        """Set flag to stop animation."""
        Animation.flag_animation_stop = True
        Animation.flag_animation_run = False

    @classmethod
    def start_animation(self):
        """Set flag to run animation."""
        Animation.flag_animation_stop = False
        Animation.flag_animation_run = True

    @staticmethod
    def rainbow_cycle(strip, wait_ms=20, iterations=1):
        """Draw rainbow that uniformly distributes itself across all pixels."""
        while (True):
            if (Animation.flag_animation_stop == True):
                Animation.color_wipe(strip, Color(0, 0, 0))
                return
            if (Animation.flag_animation_run == True):
                for j in range(256 * iterations):
                    for i in range(strip.numPixels()):
                        print("Animacja tęczy")
                        strip.setPixelColor(i, Animation.wheel(
                            (int(i * 256 / strip.numPixels()) + j) & 255))
                    strip.show()
                    time.sleep(wait_ms / 1000.0)

    @staticmethod
    def theater_chase_rainbow(strip, wait_ms=50):
        """Rainbow movie theater light style chaser animation."""
        for j in range(256):
            for q in range(3):
                for i in range(0, strip.numPixels(), 3):
                    strip.setPixelColor(i + q, Animation.wheel((i + j) % 255))
                strip.show()
                time.sleep(wait_ms / 1000.0)
                for i in range(0, strip.numPixels(), 3):
                    strip.setPixelColor(i + q, 0)

如何在第二个线程的执行过程中阻止一个线程的启动,或者如何在线程的持续时间内阻止按钮。防止两个动画同时运行。

4

0 回答 0