[英]python tkinter entry variable not defined
Can anyone help me with this error I don't know how to fix this it.谁能帮我解决这个错误,我不知道如何解决它。
from tkinter import *
def get_data():
target=Entry.get(note)
def show():
note=Entry(root)
note.grid(row=2,column=1)
b2=Button(root,text='view',command=get_data).grid(row=2,column=2)
root=Tk()
root.title("test")
Label(root,text="click").grid(row=1,column=1)
b1=Button(root,text='click',command=show).grid(row=1,column=2)
root.mainloop()
Error:错误:
target=Entry.get(note) NameError: name 'note' is not defined
The easy fix is to define note globally, then just lay it out in the function:简单的解决方法是全局定义注释,然后将其放在 function 中:
from tkinter import *
def get_data():
target=Entry.get(note)
def show():
note.grid(row=2,column=1)
b2=Button(root,text='view',command=get_data).grid(row=2,column=2)
root=Tk()
root.title("test")
note=Entry(root)
Label(root,text="click").grid(row=1,column=1)
b1=Button(root,text='click',command=show).grid(row=1,column=2)
root.mainloop()
You can pass parameters to functions triggered by buttons:您可以将参数传递给按钮触发的函数:
from tkinter import *
def get_data(note):
target = Entry.get(note)
def show(root):
note = Entry(root)
note.grid(row=2, column=1)
b2 = Button(root, text='view', command=lambda: get_data(note)).grid(row=2, column=2)
def main():
root = Tk()
root.title("test")
Label(root,text="click").grid(row=1, column=1)
b1 = Button(root, text='click', command=lambda: show(root)).grid(row=1, column=2)
root.mainloop()
if __name__ == '__main__':
main()
I've used a lambda
to make the call with the parameter note
.我使用
lambda
使用参数note
进行调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.