I added a top level in my tkinter GUI but I added a button which runs a deep learning model and is supposed to display the result in a separate label; however, when I run the code it says the top level is not defined.
def import_model(image):
model = keras.models.load_model("C:\\Users\\Haame\\Documents\\AIIP\\my_model")
image=cv2.imread(image)
image = cv2.resize(image,(196,196))
predicted_label = model.predict(image[None,...]).argmax()
#display result
result = tk.Label(new, bg="black", fg="grey81", text=predicted_label, font=("Segoe Script", 25), borderwidth=3, relief="solid").pack()
def open_win(image):
new= Toplevel(canvas)
new.geometry("1000x850")
new.title("New Window")
Label(new, text="Selected image: ", font=('Helvetica 17 bold')).pack(pady=20)
#create run button
button2 = tk.Button (new, text='Run', font=("ROG FONTS", 10), bg='green', command =
import_model(image))
button2.pack(pady=15)
I even tried passing the top level as a parameter in my function but it still did not work.
Edit: Full error
File "C:\Users\Haame\Desktop\Project Front End.py", line 44, in import_model
result = tk.Label(new, bg="black", fg="grey81", text=predicted_label, font=("Segoe Script", 25), borderwidth=3, relief="solid").pack()
NameError: name 'new' is not defined
Since new
is a local variable inside open_win()
, so it is not accessible inside import_model()
. One of the way to fix it is to pass new
as an argument to import_model()
as well.
Note also that command=import_model(image)
will execute import_model()
immediately without clicking the button. lambda
should be used in this case.
Below are the suggested changes:
# added new argument 'parent'
def import_model(parent, image):
model = keras.models.load_model("C:\\Users\\Haame\\Documents\\AIIP\\my_model")
image=cv2.imread(image)
image = cv2.resize(image,(196,196))
predicted_label = model.predict(image[None,...]).argmax()
#display result
# used 'parent' argument
tk.Label(parent, bg="black", fg="grey81", text=predicted_label, font=("Segoe Script", 25), borderwidth=3, relief="solid").pack()
def open_win(image):
new= Toplevel(canvas)
new.geometry("1000x850")
new.title("New Window")
Label(new, text="Selected image: ", font=('Helvetica 17 bold')).pack(pady=20)
#create run button
button2 = tk.Button (new, text='Run', font=("ROG FONTS", 10), bg='green',
# used lambda and pass 'new' to import_model() as well
command = lambda: import_model(new, image))
button2.pack(pady=15)
Another suggestion is to create the result label inside open_win()
and pass this label to import_model()
:
def import_model(image, result):
model = keras.models.load_model("C:\\Users\\Haame\\Documents\\AIIP\\my_model")
image=cv2.imread(image)
image = cv2.resize(image,(196,196))
predicted_label = model.predict(image[None,...]).argmax()
#display result
result.config(text=predicted_label)
result.pack()
def open_win(image):
new= Toplevel(canvas)
new.geometry("1000x850")
new.title("New Window")
Label(new, text="Selected image: ", font=('Helvetica 17 bold')).pack(pady=20)
#create run button
button2 = tk.Button (new, text='Run', font=("ROG FONTS", 10), bg='green',
command = lambda: import_model(image, result))
button2.pack(pady=15)
# create the result label and make it hidden initially
result = tk.Label(new, bg="black", fg="grey81", font=("Segoe Script", 25), borderwidth=3, relief="solid")
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.