How to solve NameError in python - python

I'm invoking one function using a button click. But it is giving me error: NameError: global name 'new' is not defined ,even though I have defined new as a new window.
My code for a new window:
def result():
root.withdraw()
new = Toplevel()
new.geometry("1105x605+300+300")
button3 = Button(new, text='Select a Query Image',command = matching_image)
button3.pack(padx = 1, pady = 1,anchor='ne')
button3.place( x = 570, y = 60)
The button will invoke matching_image function, and the code will be:
def matching_image():
path1 = tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
im = Image.open(path1)
resized = im.resize((200, 200),Image.ANTIALIAS)
tkimage = ImageTk.PhotoImage(resized)
myvar1 = Label(new,image = tkimage)
myvar1.image = tkimage
myvar1.pack()
myvar1.place(x = 30, y = 100)
And this is giving the error. The error message is as follows:
%run "D:/6th sem/Major project/Code/frame.py"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.0.3.1262.win-x86\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "D:\6th sem\Major project\Code\frame.py", line 194, in matching_image
myvar1 = Label(new,image = tkimage)
NameError: global name 'new' is not defined
Any suggestions! So that I can solve this error.
Thanks in advance :)

You defined new in a function, so that variable only exists in that function's scope. Therefore, when you try to access it in another scope (here it is the global scope) you will get a NameError as it is not available at that level.
You can fix this by doing global new at the start of the function in which you define it.
This statement puts it in the global scope, meaning that it is defined at the module level. Therefore, you can access it anywhere in the program and you will not get that error.
Like this:
def result():
global new
new = Toplevel()
# Other stuff.
Though note that doing global declarations is considered bad practice. Much better to put your code in class form and assign applicable variables to self.

Your problem is condensed down to this:
def foo():
var = 1
def bar():
print var
foo()
bar()
The principle problem you have is that 'var' is a local, not a global.
This code example works:
def foo():
global var
var = 1
def bar():
print var
foo()
bar()
Because var is declared to be global instead.

Related

Calling function from another Python file

I am trying to call a function from another Python file after a button is click. I have imported the file and used the FileName.fuctionName() to run the function. The problem is my exception keeps catching. I am guessing that the data from the function being called is not being grabbed.What I am trying to do is have a user fill out a Tkinter gui then click a button. Once the button is click the user will then be asked to scan their tag (rfid) and that data will then be sent to a firebase real time database which will store the user's inputted info along with the card_id and user_id that was created when the tag was scanned.
Im kinda at a loss because other than the exception catching I am not getting any other errors, any thoughts? I have posted the code below along with comments.
error : local variable 'user_id' referenced before assignment
from tkinter import *
#Second File
import Write
from tkcalendar import DateEntry
from firebase import firebase
data = {}
global user_id
# Firebase
firebase= firebase.FirebaseApplication("https://xxxxxxx.firebaseio.com/",None)
# button click
def sub ():
global user_id
#setting Variables from user input
name = entry_1.get()
last = entry_2.get()
number = phone.get()
try:
#Calling Function from other file
Write.scan()
if Write.scan():
#getting the New User Id
user_id= new_id
#User Info being sent to the Database
data = {
'Name #': name,
'Last': last,
'Number': number,
'Card #':user_id
}
results = firebase.post('xxxxxxxx/User',data)
except Exception as e:
print(e)
# setting main frame
root = Tk()
root.geometry('850x750')
root.title("Registration Form")
label_0 = Label(root, text="Registration form",width=20,font=("bold", 20))
label_0.place(x=280,y=10)
label_1 = Label(root, text="First Name",width=20,font=("bold", 10))
label_1.place(x=80,y=65)
entry_1 = Entry(root)
entry_1.place(x=240,y=65)
label_2 = Label(root, text="Last Name",width=20,font=("bold", 10))
label_2.place(x=68,y=95)
entry_2 = Entry(root)
entry_2.place(x=240,y=95)
phoneLabel = Label(root, text="Contact Number : ",width=20,font=("bold", 10))
phoneLabel.place(x=400,y=65)
phone = Entry(root)
phone.place(x=550,y=65)
Button(root, text='Submit',command = sub,width=20,bg='brown',fg='white').place(x=180,y=600)
root.mainloop()
Write.py file being Imported
import string
from random import*
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
#Function being called
def scan():
try:
#Creating user hash
c = string.digits + string.ascii_letters
new_id = "".join(choice(c) for x in range(randint(25,25)))
print("Please Scan tag")
#Writing to tag
reader.write(new_id)
if reader.write(new_id):
print("Tag Scanned")
else:
print("Scan Tag First")
print("Scanning Complete")
finally:
GPIO.cleanup()
I see that the value new_id in one file isn't going to influence the value with the same name in the other file, for a similar reason as for the first problem. In both places it appears, new_id is a local variable that only exists in the enclosing function.
Another issue I see is that you're calling Write.scan() twice in a row. Do you mean to be calling it twice? I expect not.
Also, you're testing the return value of Write.scan(), but that function doesn't return a value. So I think that the code in the if block in the first file will never run.
Globals are a bad idea in general, as they're easy to get wrong and they tend to obscure what the code is really doing. "Never say never", but I'll say that I very rarely find the need for a global variable in Python. In your case, I think it would be much better to have Write.scan() return the value of the new user id instead of passing it back as a global. Since you're testing the value of Write.scan(), maybe this is what you were thinking of doing already. Here are the changes I'd make to address these three issues and hopefully get your code working the way you want...
...
def sub ():
...
try:
#Calling Function from other file
new_id = Write.scan()
if new_id:
#getting the New User Id
user_id= new_id
...
...
def scan():
try:
...
new_id = "".join(choice(c) for x in range(randint(25,25)))
...
return new_id
finally:
GPIO.cleanup()
It's impossible to tell what your problem is, because there is no place in your code that references user_id and hence the error message you cite can't come from the code you provide. However, I see a pretty common mistake that your code appears to be making that could very well account for why you expect user_id to be defined somewhere in your code and yet it is not...
In your first block of code, the global user_id is not being set by the sub function. Rather, when the sub function calls user_id=new_id, it is creating and setting a variable that is local to that function. When that function ends, the result of that call is lost and the global user_id is still undefined.
What you want is to define user_id as being global inside of the sub() function. Just add global user_id anywhere near the top of the function definition.
Here's an example of what I'm talking about:
global x
def sub():
x = 3
sub()
print(x)
result:
Traceback (most recent call last):
File "t", line 7, in <module>
print(x)
NameError: global name 'x' is not defined
whereas:
global x
def sub():
global x
x = 3
sub()
print(x)
result:
3

Python Tkinter error entryformartname is not defined

Main module calls the module which I have posted. I am trying to create a point of sale and this particular code renames the title of the program.
Here is my code:
from Globalvariables import *
from tkinter import *
#Mart Name
#user accounts
def settingsmain():
settingmenu = Toplevel()
settingmenu.iconbitmap('D:/Gatlabs logo.ico')
labelformartname = Label(settingmenu, text = "Enter name of your store")
entryformartname = Entry(settingmenu)
entryformartname.grid(row = 0, column = 0)
setmartname = Button(settingmenu, text = "setname", command = setname)
setmartname.grid(row= 0, column = 1)
settingmenu.mainloop()
def setname():
global Mname
Mname = entryformartname.get()
Although I have imported Tkinter and created an entry widget, I get error:
"NameError: name 'entryformartname' is not defined"
Where am I mistaken?
As the entryformartname has been defined in a different function and in the line -:
Mname = entryformartname.get()
you're tryna access it in a different function, it gives the error entryformartname not defined.
Now, as the entryformartname is a local variable of only the specific function where it's defined, so if you wanna use it in some other function or somewhere else generally, you'll have to make it a global var.
It's really simple to make it a global variable. Just use the keyword global like so in the function where it's defined -:
global entryformartname # The code is used to declare that entryformartname is a global variable.
This should make it global. But still until you've told the other function, that the entryformartname variable you wanna access is global and not local you have to import it into the other function which can be done like so -:
global entryformartname # The same code but this time used for importing the global var.
So adding these lines in both functions should solve your problem.
Hope it helps
And hope you're safe in the times of an ongoing pandemic.
You could use a global variable.like:
def settingsmain():
global entryformartname
settingmenu = Toplevel()
settingmenu.iconbitmap('D:/Gatlabs logo.ico')
labelformartname = Label(settingmenu, text="Enter name of your store")
entryformartname = Entry(settingmenu)
entryformartname.grid(row=0, column=0)
setmartname = Button(settingmenu, text="setname", command=setname)
setmartname.grid(row=0, column=1)
settingmenu.mainloop()
def setname():
global Mname,entryformartname
Mname = entryformartname.get()

How to get scale value with button press tkinter?

I really did my best to find the solution on my own, but haven't. I want to have the value from a slider and then save it to a csv file (which is working fine), at the click of a button. Alas, I can't get the value of the tkinter.Scale during my button event. I wonder if it global variables might solve my problem, but I haven't gotten them to work. I'm particularly surprised because I can print a live stream of the scale's value as I change it, but can't save it in a useful way. If you could answer any of my befuddlement or let me know if my question is unclear or in anyway could be better, I'd greatly appreciate it. Here are some links to things that helped me get this far:
https://www.programiz.com/python-programming/global-local-nonlocal-variables
Tkinter - Get the name and value of scale/slider
Here is my attempt to print the final value 10 times:
from tkinter import *
root = Tk()
def scaleevent(v): #Live update of value printed to shell
print(v)
variable = v
def savevalue():
global variable #This is what I want to work, but doesn't
for i in range(10):
print(variable)
scale = Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
button = Button(text="button", command=savevalue).grid(column=1, row=0)
root.mainloop()
And here is my attempt to solve my problem using .get():
from tkinter import *
root = Tk()
def savevalue(): #print value 10 times. In final version I will save it instead
for i in range(10):
print(scale.get()) #I really want this to work, but it doesn't,
root.destroy #is it because .get is in a function?
scale = Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
button = Button(text="button", command=savevalue).grid(column=1, row=0)
root.mainloop()
(Python 3.5, Windows 10)
Edit:
This is the error I get from the first attempt using a global variable:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Me\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
File "C:\Users\Me\Documents\programing\tkinter scale question.py", line 15, in savevalue
print(variable)
NameError: name 'variable' is not defined
That's what happened when I run the first example of code, and similarly my actual project. Thanks Bryan Oakley!
You have to use global in scaleevent because you try to assing value to variable. Without global it assigns v to local variable and then it doesn't exists in savevalue
from tkinter import *
root = Tk()
def scaleevent(v):
global variable
print(v)
variable = v
def savevalue():
print(variable)
Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
Button(text="button", command=savevalue).grid(column=1, row=0)
root.mainloop()
As for second version you made mistake with var = Widget(...).grid()
It assigns None to var because grid()/pack()/place() returns None.
You have to do it in two lines:
var = Widget(...)
var.grid(...)
Code
from tkinter import *
root = Tk()
def savevalue():
print(scale.get())
root.destroy() # you forgot ()
scale = Scale(orient='vertical')
scale.grid(column=0,row=0)
button = Button(text="button", command=savevalue)
button.grid(column=1, row=0)
root.mainloop()
from tkinter import *
root = Tk()
variable=0 # only you forgot this
def scaleevent(v):
print(v)
global variable
variable=v
def savevalue():
print(variable)
Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
Button(text="button", command=savevalue).grid(column=1, row=0)
root.mainloop()

can't access global variable from inside a function in python

Below is my code
global PostgresDatabaseNameSchema
global RedShiftSchemaName
PostgresDatabaseNameSchema = None
RedShiftSchemaName = None
def check_assign_global_values():
if not PostgresDatabaseNameSchema:
PostgresDatabaseNameSchema = "Superman"
if not RedShiftSchemaName:
RedShiftSchemaName = "Ironman"
check_assign_global_values()
But i am getting an error saying
Traceback (most recent call last):
File "example.py", line 13, in <module>
check_assign_global_values()
File "example.py", line 8, in check_assign_global_values
if not PostgresDatabaseNameSchema:
UnboundLocalError: local variable 'PostgresDatabaseNameSchema' referenced before assignment
So can't we access or set the global variables from inside a function ?
global should always be defined inside a function, the reason for this is because it's telling the function that you wanted to use the global variable instead of local ones. You can do so like this:
PostgresDatabaseNameSchema = None
RedShiftSchemaName = None
def check_assign_global_values():
global PostgresDatabaseNameSchema, RedShiftSchemaName
if not PostgresDatabaseNameSchema:
PostgresDatabaseNameSchema = "Superman"
if not RedShiftSchemaName:
RedShiftSchemaName = "Ironman"
check_assign_global_values()
You should have some basic understanding of how to use global. There is many other SO questions out there you can search. Such as this question Using global variables in a function other than the one that created them.

Unbound Local Error with global variable [duplicate]

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 7 months ago.
I am trying to figure out why I get an UnboundLocalError in my pygame application, Table Wars. Here is a summary of what happens:
The variables, REDGOLD, REDCOMMAND, BLUEGOLD and BLUECOMMAND, are initialised as global variables:
#Red Stat Section
REDGOLD = 50
REDCOMMAND = 100
#Blue Stat Section
BLUEGOLD = 50
BLUECOMMAND = 100
def main():
[...]
global REDGOLD
global REDCOMMAND
global BLUEGOLD
global BLUECOMMAND
This works when spawning units within the main loop, subtracting funds to spawn units.
Right now, I am trying to set up a system so that when a unit dies, the killer refunds the victim's COMMAND and earns GOLD based on what he killed:
class Red_Infantry(pygame.sprite.Sprite):
def __init__(self, screen):
[...]
self.reward = 15
self.cmdback = 5
[...]
def attack(self):
if self.target is None: return
if self.target.health <= 0:
REDGOLD += self.target.reward #These are the problem lines
BLUECOMMAND += self.target.cmdback #They will cause the UnboundLocalError
#when performed
self.target = None
if not self.cooldown_ready(): return
self.target.health -= self.attack_damage
print "Target's health: %d" % self.target.health
This works right up until the unit dies. Then this happens:
Traceback (most recent call last):
File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 606, in <module>
main()
File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 123, in main
RedTeam.update()
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 399, in update
for s in self.sprites(): s.update(*args)
File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 304, in update
self.attack()
File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 320, in attack
REDGOLD += self.target.reward
UnboundLocalError: local variable 'REDGOLD' referenced before assignment
How do I get the global variables mentioned above to change with the attack block? If it helps, I am using Pygame 2.7.x, so nonlocal won't work :/
global make the global variable visible in the current code block. You only put the global statement in main, not in attack.
ADDENDUM
Here is an illustration of the need to use global more than once. Try this:
RED=1
def main():
global RED
RED += 1
print RED
f()
def f():
#global RED
RED += 1
print RED
main()
You will get the error UnboundLocalError: local variable 'RED' referenced before assignment.
Now uncomment the global statement in f and it will work.
The global declaration is active in a LEXICAL, not a DYNAMIC scope.
You need to declare the variable as global in each scope where they are being modified
Better yet find a way to not use globals. Does it make sense for those to be class attributes for example?
Found that variables in main act like global "read only" variables in function. If we try to reassign the value, it will generate error.
Try:
#!/usr/bin/env python
RED=1
A=[1,2,3,4,5,6]
def f():
print A[RED]
f()
It's ok.
But:
#!/usr/bin/env python
RED=1
A=[1,2,3,4,5,6]
def f():
print A[RED]
A = [1,1,1,1,1]
f()
Generate
File "./test.py", line 6, in f
print A[RED]
UnboundLocalError: local variable **'A'** referenced before assignment
and:
#!/usr/bin/env python
RED=1
A=[1,2,3,4,5,6]
def f():
print A[RED]
RED = 2
f()
Generate
File "./test.py", line 6, in f
print A[RED]
UnboundLocalError: local variable **'RED'** referenced before assignment

Categories

Resources