How I can solved this name is not defined - python

I want to make the program that I made can run
i make code like this
def cafe_food(self):
friedrice_items = tk.Entry(F_FItems)
friedrice_items.config(width=7, borderwidth=4, relief="sunken", font=("calibri", 10,"bold"),foreground="white", background="#248aa2")
friedrice_items.place(x=81, y=1)
def Total_Biil(self):
friedrice_price = 10
pizza_price = 20
if friedrice_items.get() != "":
friedrice_cost = friedrice_price * int(friedrice_items.get())
else:
friedrice_cost = 0
if pizza_items.get() != "":
friedrice_cost = pizza_price * int(pizza_items.get())
else:
pizza_cost = 0
total_bills = friedrice_cost + pizza_cost
if i run this code and..
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\kisah tegar\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
File "D:\Code\Python\Project\restaurant_mg\main.py", line 498, in Total_Bill
if friedrice_items.get() != "":
NameError: name 'friedrice_items' is not defined
[Finished in 6.3s]
this my problem:(
how can i get friedrice_items in that function

If this piece of code is in a class, try adding self. before you variable names:
def cafe_food(self):
self.friedrice_items = tk.Entry(F_FItems)
self.friedrice_items.config(width=7, borderwidth=4, relief="sunken", font=("calibri", 10,"bold"),foreground="white", background="#248aa2")
self.friedrice_items.place(x=81, y=1)
def Total_Biil(self):
self.friedrice_price = 10
self.pizza_price = 20
if self.friedrice_items.get() != "":
self.friedrice_cost = self.friedrice_price * int(self.friedrice_items.get())
else:
self.friedrice_cost = 0
if self.pizza_items.get() != "":
self.friedrice_cost = self.pizza_price * int(self.pizza_items.get())
else:
self.pizza_cost = 0
self.total_bills = self.friedrice_cost + self.pizza_cost
self or whatever you name it, is a global keyword, where in a class, you can access the variable anywhere inside the class.
If you don't use self you would not be able to access it anywhere, like what happened to you before.

Related

What causes an exception in Tkinter callback? [duplicate]

This question already has an answer here:
TypeError: 'NoneType' object is not callable with tkinter
(1 answer)
Closed 1 year ago.
I'm trying to make a program that you can add infinite rooms to, so all of my code is built around using one variable to deduce which room is which. However when I run it, it gives me an error that doesn't directly reference any one line in my code, and since I'm a pretty new programmer, I don't know what it means. Also my code is pretty all over the place and incomplete. Thanks for any help!
The error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\SCA0023\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
TypeError: 'NoneType' object is not callable
The Code
from tkinter import *
root = Tk()
class Room:
def __init__(self, items):
self.objects = []
self.objects.append(items)
def list(self):
print(self.objects)
def addkitchenmenu(r): #add a new option menu attributed to a new room
globals()[f'kitchenvar_{r}'] = StringVar(root)
globals()[f'kitchenvar_{r}'].set("Add an appliance")
globals()[f'kitchenvar_{r}'].trace('w', applianceadd(r))
kitchenitems = ['Kettle', 'Toaster']
globals()[f'appliancelist_{r}'] = OptionMenu(root, globals()[f'kitchenvar_{r}'], *kitchenitems).pack()
addkitchen(r)
def applianceadd(r): #add a new room
globals()[f'kobjects_{r}'] = []
globals()[f'kobjects_{r}'].append(globals()[f'kitchenvar_{r}'].get())
items = globals()[f'kobjects_{r}']
globals()[f'kroom_{r}'] = Room(items)
globals()[f'kroom_{r}'].list()
def addkitchen(r): #add an appliance
globals()[f'addappliace{r}'] = Button(root, text='add appliance', command=lambda: applianceadd(r))
def newkitchencheck(): #find the next name for a room that isn't taken
varnotfound = True
a = 0
while varnotfound:
if f'kroom{a}' in globals():
a += 1
else:
r = a
varnotfound = False
addkitchenmenu(r)
addroombutton = Button(root, text="add kitchen", command=newkitchencheck)
addroombutton.pack()
root.mainloop()
You are passing result of applianceadd(r) (which is None) to .trace(). Change to .trace("w", lambda *_: applianceaddr(r)).

class aggregation (classes within classes) with Python 3 and Tkinter

It says that base_obj is not defined. But I did define it already. So why am I getting this error?
here is the code:
from tkinter import *
root = Tk()
class BaseClass:
def __init__(self,an_int,a_string):
self.the_int = an_int
self.the_string = a_string
class BiggerClass:
def __init__(self,an_instance_of_BaseClass,big_class_string,big_class_int,new_name):
self.the_instance_of_BaseClass = an_instance_of_BaseClass #here we are aggregating the base class into the bigger class
self.the_big_class_string = big_class_string
self.the_big_class_int = big_class_int
self.the_big_class_new_name = new_name
base_int_var = IntVar()
base_string_var = StringVar()
bigger_name_var = StringVar()
entry_base_int = Entry(root,textvariable = base_int_var).pack()
entry_base_string = Entry(root,textvariable = base_string_var).pack()
big_new_name_var = StringVar()
entry_bigger_name = Entry(root, textvariable = bigger_name_var).pack()
entry_big_new_name = Entry(root,textvariable = big_new_name_var).pack()
def create_base_class_instance():
global base_obj
base_obj = BaseClass(base_int_var.get(),base_string_var.get()) # I define 'base_obj' here
list_of_bigs = []
def create_bigger_class_instance(big_handle):
bigger_name_var = big_handle
big_handle = BiggerClass(base_obj,bigger_name_var.get(),55,big_new_name_var.get())
list_of_bigs.append(big_handle)
#global big_obj
#big_obj = BiggerClass(base_obj,bigger_name_var.get(),45)
create_base_class_button = Button(root, text ="create base class", command = create_base_class_instance).pack()
create_big_class_button = Button(root, text ="create big class", command = create_bigger_class_instance(big_new_name_var)).pack()
match_name_var = StringVar()
entry_match_name = Entry(root,textvariable = match_name_var).pack()
def my_button_method():
for a_big in list_of_bigs:
if a_big.the_big_class_new_name == match_name_var:
print(a_big.the_instance_of_BaseClass.the_string)
#print(big_obj.the_instance_of_BaseClass.the_int)
#bigger_class_obj = BiggerClass(base_obj,"hello this is the big class",45)
button_print_out = Button(root,text = "press me", command = my_button_method).pack()
root.mainloop()
here is the error message:
Traceback (most recent call last):
File "C:/Users/TOTTY/PycharmProjects/my game/aggregation practice fork 1.py", line 45, in <module>
create_big_class_button = Button(root, text ="create big class", command = create_bigger_class_instance(big_new_name_var)).pack()
File "C:/Users/TOTTY/PycharmProjects/my game/aggregation practice fork 1.py", line 39, in create_bigger_class_instance
big_handle = BiggerClass(base_obj,bigger_name_var.get(),55,big_new_name_var.get())
NameError: name 'base_obj' is not defined
You have defined the object in create_base_class_instance function and you are calling it in my_button_method.
You should initialize it outside, and use global keyword in both functions.
However using global variables considered code smell. I would advise finding another solution, for example passing base_obj as an argument to both functions.
base_obj = None
def some_function():
global base_obj
# some code referencing base_obj
def other_function():
global base_obj
# some code referencing base_obj
Functions in Python are executed only when they are called. The keyword global is used to indicate that the variable used here is the same as in the global scope. Thus you will need to add a declaring statement in the main class and not in any of the sub function.
For e.g. You will have to write
base_obj = None
In the main class before either of the two functions is called. You do not need global base_obj in your second function as you are not assigning any value to it.
Look at this line of code:
create_big_class_button = Button(..., command = create_bigger_class_instance(big_new_name_var)).pack()
You are immediately calling create_bigger_class_instance(...), and the result of that is getting assigned to the command. Since create_bigger_class_instance relies on the existence of base_obj, and you haven't created base_obj yet since it's tied to a button click, you get the error.
(As a side note, doing something like create_big_class_button = Button(...).pack() will always result in create_big_class_button being set to None, because that is what pack() returns.)

Python :TypeError: this constructor takes no arguments

When the user enters an email address, and the program reads the email and display it according to its criteria (e.g yeo.myy#edu.co), like criteria:
username is yeo.myy
domain is edu.co
I know its something to do with the "#".
this is the code
class Email:
def __int__(self,emailAddr):
self.emailAddr = emailAddr
def domain(self):
index = 0
for i in range(len(emailAddr)):
if emailAddr[i] == "#":
index = i
return self.emailAddr[index+1:]
def username(self):
index = 0
for i in range(len(emailAddr)):
if emailAddr[i] == "#" :
index = i
return self.emailAddr[:index]
def main():
emailAddr = raw_input("Enter your email>>")
user = Email(emailAddr)
print "Username = ", user.username()
print "Domain = ", user.domain()
main()
this is the error I got:
Traceback (most recent call last):
File "C:/Users/Owner/Desktop/sdsd", line 29, in <module>
main()
File "C:/Users/Owner/Desktop/sdsd", line 24, in main
user = Email(emailAddr)
TypeError: this constructor takes no arguments
def __int__(self,emailAddr):
Did you mean __init__?
def __init__(self,emailAddr):
You're also missing a couple selfs in your methods, and your returns are improperly indented.
def domain(self):
index = 0
for i in range(len(self.emailAddr)):
if self.emailAddr[i] == "#":
index = i
return self.emailAddr[index+1:]
def username(self):
index = 0
for i in range(len(self.emailAddr)):
if self.emailAddr[i] == "#" :
index = i
return self.emailAddr[:index]
Result:
Username = yeo.myy
Domain = edu.co
Incidentally, I recommend partition and rpartition for splitting a string into two pieces on a given separator. Sure beats keeping track of indices manually.
def domain(self):
return self.emailAddr.rpartition("#")[2]
def username(self):
return self.emailAddr.rpartition("#")[0]
This error may happen if you type def _init_ with a single underline instead of def __init__ with double underlines before and after init.
class Employee:
def __init__(self,Name,Age,Salary,Gender):
self.Name = Name
self.Age = Age
self.Salary= Salary
self.Gender = Gender
def show_employee_deatils(self):
print("Name of the employee is ",self.Name)
print("Age of the employee is ",self.age)
print("Salary of the employee is ",self.salary)
print("gender of the employee is ",self.gender)
e1 = Employee('Shubham',25,25000,'male')
e1. show_Employee_deatils( )

AttributeError: 'int' object has no attribute Python

I dont know why i get this error and it's really annoying... anyone see the problem?
I get this error:
line 66, in <module>
ting.movefigure(ting, "up", 20)
AttributeError: 'int' object has no attribute 'movefigure'
Here is my code:
from tkinter import * import time
def movefigure(self, direction, ammount):
x = 0
y = 0
ammount2 = 0
if direction == "up":
print("Direction = " + ammount)
y = ammount
elif direction == "down":
print("Direction = " + ammount)
ammount2 = ammount - (ammount * 2)
y = ammount2
elif direction == "right" + ammount:
print("Direction = " + ammount)
x = ammount
elif direction == "left":
print("Direction = " + ammount)
ammount2 = ammount - (ammount * 2)
y = ammount2
canvas.move(self, x, y)
root = Tk()
root.title('Canvas')
tingx = 100
tingy = 100
tingxMove = 1
tingyMove = 1
canvas = Canvas(root, width=400, height=400)
ting = canvas.create_rectangle(205, 10, tingx, tingy, tags="Ting", outline='black', fill='gray50')
canvas.pack()
ting.movefigure(ting, "up", 20)
root.mainloop()
You're mixing up functions and methods.
A method is a function defined in a class; it takes a self argument, and you call it on an instance of that class. Like this:
class Spam(object):
def __init__(self, eggs):
self.eggs = eggs
def method(self, beans):
return self.eggs + beans
spam = Spam(20)
print(spam.method(10))
This will print out 30.
But your movefigure is not a method of any class, it's just a regular function. That means it doesn't take a self parameter, and you don't call it with dot syntax. (Of course there's nothing stopping you from calling any parameter self if you want, just like there's nothing stopping you from writing a function called print_with_color that erases a file named /kernel, but it's not a good idea…)
So, you wanted to do this:
def movefigure(rect, direction, ammount):
# all of your existing code, but using rect instead of self
movefigure(ting, "up", 20)

python NameError variable already set to global

I want to Change the Position of a spinbox and an entry field by clicking on a button.
Clicking on the button Triggers this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "GUI.py", line 79, in changesite
if (site==0):
NameError: global name 'site' is not defined
My code is this here:
else:
w = Spinbox(dialog, from_=5, to=getmodulo(ceiling), increment = 5.0, state='readonly',font = "bold")
e = Entry(dialog,font = "bold")
e.place(x=390,y=120)
w.place(x=20,y=120)
site = 0
def changesite():
global site
if (site==0):
e.destroy()
w.destroy()
ws = Spinbox(dialog, from_=5, to=getmodulo(ceiling), increment = 5.0, state='readonly',font = "bold")
es = Entry(dialog,font = "bold")
es.place(x=20,y=120)
ws.place(x=390,y=120)
site = 1
if (site ==1):
ws.destroy()
es.destroy()
w = Spinbox(dialog, from_=5, to=getmodulo(ceiling), increment = 5.0, state='readonly',font = "bold")
e = Entry(dialog,font = "bold")
e.place(x=390,y=120)
w.place(x=20,y=120)
site = 0
As you see I use global for site, so how can I fix this error? I don't know how to do at the Moment.
All the best;
This is sort of a wild guess, since you do not show us all of your code, but I could reproduce the problem when the "global" variable is not really global, but defined in an enclosing function.
Minimal example:
# global f is expected here...
def foo():
f = 42 # ... but defined here
def bar():
global f
f = f + 1 # this causes a NameError
bar()
foo()
Instead, you could make site a member of the class (if any), or even of the outer method (like name_of_outer_method.site), or make it truly global, or use a modifyable wrapper type.

Categories