-1

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
MaxPowers
  • 5,235
  • 2
  • 44
  • 69

2 Answers2

1

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()
Novel
  • 13,406
  • 2
  • 25
  • 41
  • As a programmer you should try very, *very*, **very** hard not to use globals. – quamrana Dec 04 '20 at 21:26
  • @quamrana Not everyone on SO is a professional programmer ... Sometimes we need to bend the rules to allow the hobbyists to get by without knowing OOP. – Novel Dec 04 '20 at 21:29
  • Professional/hobbyist? You're still a programmer. Hobbyists probably try harder than professionals. Plus this is not an OOP thing. – quamrana Dec 04 '20 at 21:30
  • @quamrana Do you have any better suggession ? – Anirban Singha Dec 04 '20 at 21:31
  • @Novel it does fix my problem. I'm still trying to figure out what's going on. – Anirban Singha Dec 04 '20 at 21:32
  • @quamrana Ok, go on then, add an answer with your non global solution. I expect you will avoid the global root as well. – Novel Dec 04 '20 at 21:32
  • This is a duplicate question. Besides I'm not banning globals, its just that there are so many ways of avoiding them. – quamrana Dec 04 '20 at 21:33
  • 1
    @AnirbanSingha it's a "scope" problem. You can't use a variable that you defined inside the function outside the function. – Novel Dec 04 '20 at 21:34
  • @Novel will you explain your answer please? I think both both program do exactly same except printing before and after. correct me if i'm wrong – Anirban Singha Dec 04 '20 at 21:35
  • @AnirbanSingha the key is that I defined the "note" variable outside of the function. This makes it "global". Therefore you can access it from both functions. In your code you defined it inside a function. When you define something inside a function it's only available to use in the function, unless you pass it out. This is called function "scope". For beginner questions like this try a beginner forum like learnpython.reddit.com where people can explain it better. – Novel Dec 04 '20 at 21:38
1

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.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • As a programmer you should try very, very, very hard not to use globals.... I thought you were going to show without globals? – Novel Dec 04 '20 at 21:41
  • Your code raised `NameError: name 'root' is not defined` on the line `note = Entry(root)` inside `show()` because `root` is a local variable inside `main()`. – acw1668 Dec 05 '20 at 11:30
  • Ok, same fix coming up,,, – quamrana Dec 05 '20 at 11:32