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:
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.