试图让这取决于声音改变颜色的交通信号灯(响亮的声音=红色,正常声音=绿色等)

问题描述 投票:-1回答:2

我一直在试图让一个Tkinter的应用程序,可以作为对所产生的声音反馈红绿灯。因此,例如,当其在库中越来越响亮你得到了红灯,但是当其正常那么它的绿色环保等特点。

基本上,我创建了一个Tkinter的应用程序用帆布和矩形(交通灯)由3个圈也做了功能,从你的麦克风获取输入,看看如何高或低的声音。

此代码是在一个单独的文件,以获得输入声音制作:

def decide_colour():

    def print_sound(indata, outdata, frames,tijd, status):
        global colour
        volume_norm = np.linalg.norm(indata)
        print(volume_norm)
        time.sleep(1)

        #set fill colour
        if volume_norm > 2 and volume_norm <4:
            colour = "yellow"
        elif volume_norm > 4:
            colour = "red"
        else:
            colour = "green"

        print(colour)

    with sd.Stream(callback=print_sound):
        sd.sleep(duration * 1000)


decide_colour()

这是Tkinter的应用程序,应该显示它:

class TrafficLights:

    def __init__(self):

        root = Tk()
        root.title("Stoplicht")
        root.configure(bg='black')
        root.geometry('460x400')

        # Frame voor widgets
        frame = Frame(root)
        frame.grid()

        self.colour = StringVar()

        # canvas voor lichten
        self.canvas = Canvas(root, width=460, height=400, bg="black")
        self.canvas.create_rectangle(190, 10, 310, 350, outline='white', fill='black')
        self.canvas.grid()

        self.oval_red = self.canvas.create_oval(200, 20, 300, 120, fill="white")

        self.oval_yellow = self.canvas.create_oval(200, 130, 300, 230, fill="white")

        self.oval_green = self.canvas.create_oval(200, 240, 300, 340, fill="white")

        # kleurbepaling voor de cirkels
        def change_color(self):

            if colour == 'red':
                self.canvas.itemconfig(self.oval_red, fill="red")
                self.canvas.itemconfig(self.oval_yellow, fill="white")
                self.canvas.itemconfig(self.oval_green, fill="white")
            elif colour == 'yellow':
                self.canvas.itemconfig(self.oval_red, fill="white")
                self.canvas.itemconfig(self.oval_yellow, fill="yellow")
                self.canvas.itemconfig(self.oval_green, fill="white")
            elif colour == 'green':
                self.canvas.itemconfig(self.oval_red, fill="white")
                self.canvas.itemconfig(self.oval_yellow, fill="white")
                self.canvas.itemconfig(self.oval_green, fill="green")

        change_color(self)

        root.after(500, change_color(self))
        #root.after(500, TrafficLights)

        root.mainloop()


while True:
    decide_colour()
    TrafficLights()

但是,它的死循环。并且还通过摆脱while声明它只会打开一次。但我希望它继续运行,我想它改变交通灯的颜色。我一直在尝试了几天,寻找答案。但我真的很坚持现在。

python loops canvas tkinter real-time
2个回答
0
投票

这个问题似乎是与你的程序的组织。不需要这while循环。我做的after是怎么想的工作一个简单的例子。被调用的after循环的作用应该得到它从decide_colour()功能所需要的数据。在这个例子中,这将是my_count()

from tkinter import *

class Counter(Frame):

    def __init__(self, master=None):
        self.count = 0
        super().__init__(master)
        self.grid()
        self.__create_widgets()

    def __create_widgets(self):
        self.count_label         = Label(self)
        self.count_label["text"] = str(self.count)
        self.count_label["pady"] = 5
        self.count_label.grid()


    def my_count(self):
        self.count = self.count+1
        self.count_label["text"] = str(self.count)


root = Tk()
counter = Counter(master=root)

#do you app set up here
root.title("Counter")
root.geometry('460x400')

def do_one_iteration():
    counter.my_count()
    root.after(500, do_one_iteration)
do_one_iteration()

counter.mainloop()

0
投票

还好这不是我想要的答案,但它是一个答案仍然

我把代码一起,并与一些调整我来到了这一点。

from tkinter import *
import sounddevice as sd
import numpy as np
import time

duration = 1  #


def decide_colour():
    def print_sound(indata, outdata, frames, tijd, status):
        global colour
        volume_norm = np.linalg.norm(indata)
        time.sleep(1)

        # set fill colour
        if 2 < volume_norm < 4:
            colour = "yellow"
        elif volume_norm > 4:
            colour = "red"
        else:
            colour = "green"

        print(volume_norm, colour)

    with sd.Stream(callback=print_sound):
        sd.sleep(duration * 1000)


class TrafficLights:

    def __init__(self):

        root = Tk()
        root.title("Stoplicht")
        root.configure(bg='black')
        root.geometry('460x400')

        # Frame voor widgets
        frame = Frame(root)
        frame.grid()

        self.colour = StringVar()

        # canvas voor lichten
        self.canvas = Canvas(root, width=460, height=400, bg="black")
        self.canvas.create_rectangle(190, 10, 310, 350, outline='white', fill='black')
        self.canvas.grid()

        self.oval_red = self.canvas.create_oval(200, 20, 300, 120, fill="red")
        self.oval_yellow = self.canvas.create_oval(200, 130, 300, 230, fill="white")
        self.oval_green = self.canvas.create_oval(200, 240, 300, 340, fill="white")

    def create_frame():
        decide_colour()

        if colour == 'red':
            self.canvas.itemconfig(self.oval_red, fill="red")
            self.canvas.itemconfig(self.oval_yellow, fill="white")
            self.canvas.itemconfig(self.oval_green, fill="white")
        elif colour == 'yellow':
            self.canvas.itemconfig(self.oval_red, fill="white")
            self.canvas.itemconfig(self.oval_yellow, fill="yellow")
            self.canvas.itemconfig(self.oval_green, fill="white")
        elif colour == 'green':
            self.canvas.itemconfig(self.oval_red, fill="white")
            self.canvas.itemconfig(self.oval_yellow, fill="white")
            self.canvas.itemconfig(self.oval_green, fill="green")

        create_frame()

        root.update()
        time.sleep(1)
        root.destroy()
        return


while True:
    TrafficLights()

所以,现在它会打开一个新的主循环在一秒钟后,摧毁它以前的主循环。我其实想它一个主循环更新,但无法弄清楚如何,所以如果有人知道请告诉我

© www.soinside.com 2019 - 2024. All rights reserved.