接收从按钮调用的函数返回的内容的正确方法,将其存储为函数外部的另一个变量

A proper way to receive what is returned from a function called from a button, store it as another variable outside the function

class 顶级 1: def cancel_login(自我): 消息 = tk.messagebox.askyesno(标题="Login Page", 消息="Are you sure you want to cancel Login") 如果消息: 退出()

def login(self):
    name = self.txtUser.get()
    passwd = self.txtPasswd.get()
    if name and passwd:
        if name == "Samuel" and passwd == "Password191":
            tk.messagebox.showinfo("Login Page", "Congratulations, Login was successful")
        else:
            tk.messagebox.showerror("Login Page", "Incorrect username or password")

        self.txtUser.select_clear and self.txtPasswd.select_clear()

def speak(text):
    engine = pyttsx3.init()
    engine.setProperty('rate', 150)
    engine.setProperty('volume', 1.0)
    engine.setProperty('voice', "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0")
    engine.say(text)
    engine.runAndWait()

def get_audio(self):
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")

        audio = r.listen(source, 2)
        global said
        said = ""
        try:
            said = r.recognize_google(audio)
            # said = r.recognize_sphinx(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

def __init__(self, top=None):
    '''This class configures and populates the toplevel window.
       top is the toplevel containing window.'''
    _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
    _fgcolor = '#000000'  # X11 color: 'black'
    _compcolor = '#d9d9d9'  # X11 color: 'gray85'
    _ana1color = '#d9d9d9'  # X11 color: 'gray85'
    _ana2color = '#ececec'  # Closest X11 color: 'gray92'

    top.geometry("504x240+385+176")
    top.minsize(120, 1)
    top.maxsize(1268, 765)
    top.resizable(1, 1)
    top.title("Login Page")
    top.configure(background="#d9d9d9")

    self.Label1 = tk.Label(top)
    self.Label1.place(relx=0.139, rely=0.208, height=24, width=79)
    self.Label1.configure(background="#d9d9d9")
    self.Label1.configure(disabledforeground="#a3a3a3")
    self.Label1.configure(foreground="#000000")
    self.Label1.configure(text='''Username:''')

    self.Label2 = tk.Label(top)
    self.Label2.place(relx=0.119, rely=0.375, height=24, width=104)
    self.Label2.configure(background="#d9d9d9")
    self.Label2.configure(disabledforeground="#a3a3a3")
    self.Label2.configure(foreground="#000000")
    self.Label2.configure(text='''Password:''')

    self.txtUser = tk.Entry(top)
    self.txtUser.place(relx=0.337, rely=0.208, height=20, relwidth=0.603)
    self.txtUser.configure(background="white")
    self.txtUser.configure(disabledforeground="#a3a3a3")
    self.txtUser.configure(font="TkFixedFont")
    self.txtUser.configure(foreground="#000000")
    self.txtUser.configure(insertbackground="black")

    self.txtPasswd = tk.Entry(top)
    self.txtPasswd.place(relx=0.337, rely=0.375, height=20, relwidth=0.603)
    self.txtPasswd.configure(background="white")
    self.txtPasswd.configure(disabledforeground="#a3a3a3")
    self.txtPasswd.configure(font="TkFixedFont")
    self.txtPasswd.configure(foreground="#000000")
    self.txtPasswd.configure(insertbackground="black")

    self.btn_cancel = tk.Button(top)
    self.btn_cancel.place(relx=0.337, rely=0.583, height=24, width=47)
    self.btn_cancel.configure(activebackground="#ececec")
    self.btn_cancel.configure(activeforeground="#000000")
    self.btn_cancel.configure(background="#d9d9d9")
    self.btn_cancel.configure(disabledforeground="#a3a3a3")
    self.btn_cancel.configure(foreground="#000000")
    self.btn_cancel.configure(highlightbackground="#d9d9d9")
    self.btn_cancel.configure(highlightcolor="black")
    self.btn_cancel.configure(pady="0")
    self.btn_cancel.configure(text='''Cancel''')
    self.btn_cancel.configure(command=self.cancel_login)

    self.btn_login = tk.Button(top)
    self.btn_login.place(relx=0.556, rely=0.583, height=24, width=41)
    self.btn_login.configure(activebackground="#ececec")
    self.btn_login.configure(activeforeground="#000000")
    self.btn_login.configure(background="#d9d9d9")
    self.btn_login.configure(disabledforeground="#a3a3a3")
    self.btn_login.configure(foreground="#000000")
    self.btn_login.configure(highlightbackground="#d9d9d9")
    self.btn_login.configure(highlightcolor="black")
    self.btn_login.configure(pady="0")
    self.btn_login.configure(text='''Login''')
    self.btn_login.configure(command=self.get_audio)

if name == 'main': vp_start_gui()

不用担心我为什么要从登录按钮调用函数,但主要问题是我找不到有效的方法来保存从 get_audio() 返回的 "said"函数并将其存储为 text="the returned said word"

我们将不胜感激任何帮助或推荐。几天来我一直被这个问题困扰,似乎找不到出路。我试着让 said 成为一个全局变量,但是当我在函数外调用它时它没有传递它的值

我假设您的最终目标是在例如标签小部件中显示来自 r.recognize_google(audio) 的文本。

我想你可以做的是:

said = r.recognize_google(audio)
self.Label.configure(text=said)

将标签替换为您需要在其上显示文本的任何小部件。当然,您必须检查小部件是否具有 text 参数。对于文本小部件,您需要使用 .insert()

不过,您尝试使用 global 进行的操作也应该有效。也许尝试从您的代码中删除 said = ""

但是,如果您只想从函数中获取文本,例如在另一个函数中使用它,则必须将 get_audio return 文本 ( return(said) ) 然后将其用作您可能需要它的任何函数中的变量。

类似

def get_audio(self):

      ...

            try:
                said = r.recognize_google(audio)
                return(said)
            except Exception as e:
                print("Exception: " + str(e))


def do_Something(self):
        said = self.get_audio()
        ...