Home > Software engineering >  I can't switch a widget to another sub file. I get `NameError: name 'name' is not def
I can't switch a widget to another sub file. I get `NameError: name 'name' is not def

Time:10-26

I've been going crazy for hours for a seemingly simple and trivial error, that is NameError: name 'name' is not defined. name is a combobox found in the Main.py file. I would like to pass it to the B.py file, but strangely I can't recover it. To summarize the code and not dwell on it, I only use the code useful for solving the problem.

In the main.py file myproject/main I have:

from folder.B import example

name=ttk.Combobox(name_labelframe, width = 16)
name['value'] =["Element1", "Element2"]
name.place(x=6, y=5)
name.set("Select")

def function1():
    print("Everything")

btn = Button(window, text="Click", command=function1)
btn.place(x=10, y=80)

In the B.py subfile I have:

def example(name):
    if name.get():
       print("good") 

example(name)

I don't know if this is important information, but the B.py file is located inside a myproject/folder/B

I passed "name" into the function and imported the file into the main file. I don't understand why it doesn't work. Is there anything I forgot to write? Can you help me? Thank you

CodePudding user response:

In B.py. Comment out #example(name). Remove button widget. I removed import folder.

Try this:

import tkinter as tk
from tkinter import ttk
from B import example



 
root = tk.Tk()
root.title('Mike_96a')
x = 'Mike_96a'
print(x) 
_name = ttk.Combobox(root, values=[x]) 
_name.grid(column=0, row=1)
_name.current(0)

x= _name.get()
print(example(x))
root.mainloop()

In the B.py module:

def example(name):
    if name:
       print(f'Good morning {name}') 

#example('name')

Result:

enter image description here

  • Related