相对较新的编码,目前我在 python 中使用 tkinter,我在函数中使用文本小部件,并希望将输入从文本框发送到另一个函数。我的全局变量在模块级别表示未定义,那么如果它在函数内,我怎么能在模块级别定义它?
当我按下发送电子邮件按钮时,我收到此错误消息“NameError: name 'user_message_entry' is not defined”
有什么建议么?非常感谢!
最小复制:
import tkinter as tk
root = tk.Tk()
root.geometry("500x500")
def send_email():
global user_message_entry
subject = ":)"
body = user_message_entry.get("1.0", "end")
message = f"subject: {subject}\n\n {body}"
print(message)
def feedback():
feedback_window = tk.Toplevel()
feedback_window.geometry("690x650")
message_frame = tk.Frame(feedback_window)
message_frame.grid(row=0, column=0, columnspan=3)
user_message_entry = tk.Text(message_frame, height=10, width=60)
user_message_entry.grid(row=0, column=0)
send_email_button = tk.Button(feedback_window, command=send_email,
height=20, width=20, bg="yellow", text="send email")
send_email_button.grid(row=1, column=0)
open_feedback_button = tk.Button(root, command=feedback, height=20, width=20, bg="yellow", text="open feedback window")
open_feedback_button.grid(row=1, column=0)
root.mainloop()