【发布时间】:2021-05-27 15:20:22
【问题描述】:
我试图将保存的全局变量的值插入我的文本框中,但由于某种未知原因,即使保存的是全局变量,它也只能显示在分配值(字符串)的方法中。
note = Text(root)
note.place(relx=0.1,rely=0.65, relwidth=0.85, relheight=0.3)
global saved
def save_note(note):
try:
saved = note.get(1.0, END) # No problem!
messagebox.showinfo("Success!", saved)
except:
messagebox.showinfo("Error", "Can't save")
def get_note(note):
try:
note.insert(index=END, chars=saved) # Problem?!
except:
messagebox.showinfo("Error", "Can't get notes")
以下是使用命令 save_note 和 get_note 的按钮
button16 = Button(root, text="SAVE", bg='white', fg='black', command=lambda:save_note(note))
button16.place(relx=0.05, rely=0.65, relwidth=0.04, relheight=0.15)
button17 = Button(root, text="Get", bg='white', fg='black', command=lambda:get_note(note))
button17.place(relx=0.05, rely=0.80, relwidth=0.04, relheight=0.15)
在这行代码中,我无法在文本框中插入保存的值
note.insert(index=END, chars=saved) # Problem?!
在未能应用某些 OOP 后,我尝试了 saved.get()。我已经通过
检查了保存变量的值messagebox.showinfo("Success!", saved)
【问题讨论】: