python 定义 tk。StringVar() 作为全局变量来调用从另一个文件更新此变量的方法

python define tk.StringVar() as a global variable to call the method which update this Variable from another file

提问人:DreckigerDan 提问时间:6/2/2023 更新时间:6/2/2023 访问量:43

问:

我对传统知识有疑问。StringVar 在我的 GUI 中。我想使用另一个文件中的方法更新 StringVar。如果我尝试在同一个类中工作正常,但是一旦我从另一个函数调用该方法,因为我从这个文件中从我们的模型中获取预测,我想在我的 GUI 中显示预测,我得到一个 AttributeError,我的 VoiceRecorder 没有名为 phonological_var 的属性。我知道我在启动方法中初始化了phonological_var,这就是问题所在,但是如果我初始化它,例如init()或超出类的范围,那么定义tk还为时过早。StringVar 或者如果我设置为 None,它就不是 tk。StringVar 变量,在publish_emotion_label中,phonological_var在逻辑上没有 .set 函数。

这是我的代码:

import os
import wave
import time
import threading
import tkinter as tk
import pyaudio
import numpy as np


audio_array = []


class VoiceRecorder:


def launch(self):
    self.window = tk.Tk()

    self.window.geometry("1000x650")
    self.window.title("Emotional Recognition through Speech")

    self.phonological_var = tk.StringVar()
    self.linguistic_var = tk.StringVar()


    self.phonological_label = tk.Label(self.window, text="Phonological Prediction",font=("Robot", 18) )
    self.phonological_label.pack()

    self.phonological_prediction = tk.Label(self.window, borderwidth=2, relief="groove", textvariable=self.phonological_var,
                                            width=15, height=3, font=("Robot", 10))
    self.phonological_prediction.pack()


    self.linguistic_label = tk.Label(self.window, text="Linguistic Prediction",  font=("Robot", 18))
    self.linguistic_label.pack()

    self.linguistic_prediction = tk.Label(self.window,borderwidth=2, relief="groove",  textvariable=self.linguistic_var,
                                          width=15, height=3, font=("Robot", 10))
    self.linguistic_prediction.pack()




    self.recording = False
    self.window.mainloop()



def record_signal(self):
    if self.recording:
        self.recording = False
        self.button.config(fg="black")
    else:
        self.recording = True
        self.button.config(fg="red")
        print("Record started")
        threading.Thread(target=self.record).start()



def record(self):
    audio = pyaudio.PyAudio()
    stream = audio.open(format=pyaudio.paInt16, channels=1, rate=16000,
                        input=True, frames_per_buffer=512)


    start = time.time()

    while self.recording:
        #read the data from the stream
        audio_data = stream.read(1024)
        audio_array.append(np.frombuffer(audio_data, dtype=np.float32))



        passed = time.time() - start

        secs = passed % 60
        mins = passed // 60
        self.time_label.config(text=f"{int(mins):02d}:{int(secs):02d}")

        if passed >= 5:
            self.recording = False
            self.button.config(fg="black")
            #only for testing
            #self.publish_emotion_label("hallo", "moin")

    print("Record stopped")
    stream.stop_stream()
    stream.close()
    audio.terminate()

    audio_array_converted = np.concatenate(audio_array)

    from main_ser import add_data_to_queue
    add_data_to_queue(audio_array_converted)




def publish_emotion_label(self, prediction_1, prediction_2):


    self.phonological_var.set(prediction_1)
    self.linguistic_var.set(prediction_2)




if __name__ == "__main__":
    vc = VoiceRecorder()
    vc.launch()

我该如何解决?

提前致谢!

Python 变量 tkinter global

评论

0赞 JRiggles 6/2/2023
请尝试将其简化为最小的可重现示例

答: 暂无答案