尝试制作根据声音改变颜色的红绿灯(响亮的声音 = 红色,正常声音 = 绿色等)

我一直在尝试制作一个 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

但它陷入了一个循环。并且通过摆脱while声明,它只会打开一次。但我希望它继续运行,我希望它改变红绿灯的颜色。我已经尝试了几天,寻找答案。但我现在真的被困住了。


www说
浏览 177回答 2
2回答

哆啦的时光机

问题似乎与您的程序的组织有关。while不需要那个循环。我做了一个简单的例子,说明如何after工作。在after循环中调用的函数应该从decide_colour()函数中获取它需要的数据。在这个例子中,它将是my_count().from tkinter import *class Counter(Frame):&nbsp; &nbsp; def __init__(self, master=None):&nbsp; &nbsp; &nbsp; &nbsp; self.count = 0&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(master)&nbsp; &nbsp; &nbsp; &nbsp; self.grid()&nbsp; &nbsp; &nbsp; &nbsp; self.__create_widgets()&nbsp; &nbsp; def __create_widgets(self):&nbsp; &nbsp; &nbsp; &nbsp; self.count_label&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= Label(self)&nbsp; &nbsp; &nbsp; &nbsp; self.count_label["text"] = str(self.count)&nbsp; &nbsp; &nbsp; &nbsp; self.count_label["pady"] = 5&nbsp; &nbsp; &nbsp; &nbsp; self.count_label.grid()&nbsp; &nbsp; def my_count(self):&nbsp; &nbsp; &nbsp; &nbsp; self.count = self.count+1&nbsp; &nbsp; &nbsp; &nbsp; self.count_label["text"] = str(self.count)root = Tk()counter = Counter(master=root)#do you app set up hereroot.title("Counter")root.geometry('460x400')def do_one_iteration():&nbsp; &nbsp; counter.my_count()&nbsp; &nbsp; root.after(500, do_one_iteration)do_one_iteration()counter.mainloop()

料青山看我应如是

好吧,这不是我想要的答案,但它仍然是一个答案我把代码放在一起,经过一些调整,我来到了这个。from tkinter import *import sounddevice as sdimport numpy as npimport timeduration = 1&nbsp; #def decide_colour():&nbsp; &nbsp; def print_sound(indata, outdata, frames, tijd, status):&nbsp; &nbsp; &nbsp; &nbsp; global colour&nbsp; &nbsp; &nbsp; &nbsp; volume_norm = np.linalg.norm(indata)&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(1)&nbsp; &nbsp; &nbsp; &nbsp; # set fill colour&nbsp; &nbsp; &nbsp; &nbsp; if 2 < volume_norm < 4:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colour = "yellow"&nbsp; &nbsp; &nbsp; &nbsp; elif volume_norm > 4:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colour = "red"&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colour = "green"&nbsp; &nbsp; &nbsp; &nbsp; print(volume_norm, colour)&nbsp; &nbsp; with sd.Stream(callback=print_sound):&nbsp; &nbsp; &nbsp; &nbsp; sd.sleep(duration * 1000)class TrafficLights:&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; root = Tk()&nbsp; &nbsp; &nbsp; &nbsp; root.title("Stoplicht")&nbsp; &nbsp; &nbsp; &nbsp; root.configure(bg='black')&nbsp; &nbsp; &nbsp; &nbsp; root.geometry('460x400')&nbsp; &nbsp; &nbsp; &nbsp; # Frame voor widgets&nbsp; &nbsp; &nbsp; &nbsp; frame = Frame(root)&nbsp; &nbsp; &nbsp; &nbsp; frame.grid()&nbsp; &nbsp; &nbsp; &nbsp; self.colour = StringVar()&nbsp; &nbsp; &nbsp; &nbsp; # canvas voor lichten&nbsp; &nbsp; &nbsp; &nbsp; self.canvas = Canvas(root, width=460, height=400, bg="black")&nbsp; &nbsp; &nbsp; &nbsp; self.canvas.create_rectangle(190, 10, 310, 350, outline='white', fill='black')&nbsp; &nbsp; &nbsp; &nbsp; self.canvas.grid()&nbsp; &nbsp; &nbsp; &nbsp; self.oval_red = self.canvas.create_oval(200, 20, 300, 120, fill="red")&nbsp; &nbsp; &nbsp; &nbsp; self.oval_yellow = self.canvas.create_oval(200, 130, 300, 230, fill="white")&nbsp; &nbsp; &nbsp; &nbsp; self.oval_green = self.canvas.create_oval(200, 240, 300, 340, fill="white")&nbsp; &nbsp; def create_frame():&nbsp; &nbsp; &nbsp; &nbsp; decide_colour()&nbsp; &nbsp; &nbsp; &nbsp; if colour == 'red':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.canvas.itemconfig(self.oval_red, fill="red")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.canvas.itemconfig(self.oval_yellow, fill="white")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.canvas.itemconfig(self.oval_green, fill="white")&nbsp; &nbsp; &nbsp; &nbsp; elif colour == 'yellow':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.canvas.itemconfig(self.oval_red, fill="white")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.canvas.itemconfig(self.oval_yellow, fill="yellow")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.canvas.itemconfig(self.oval_green, fill="white")&nbsp; &nbsp; &nbsp; &nbsp; elif colour == 'green':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.canvas.itemconfig(self.oval_red, fill="white")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.canvas.itemconfig(self.oval_yellow, fill="white")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.canvas.itemconfig(self.oval_green, fill="green")&nbsp; &nbsp; &nbsp; &nbsp; create_frame()&nbsp; &nbsp; &nbsp; &nbsp; root.update()&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(1)&nbsp; &nbsp; &nbsp; &nbsp; root.destroy()&nbsp; &nbsp; &nbsp; &nbsp; returnwhile True:&nbsp; &nbsp; TrafficLights()所以现在它在一秒钟后打开一个新的主循环并破坏之前的主循环。我实际上希望它在一个主循环中更新,但不知道如何更新,所以如果有人知道请告诉我
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python