Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
def show():
      myLabel = Label(root, text = clicked.get()).pack()   

clicked = StringVar()  
BNdrop = OptionMenu(root, clicked, "abc", "xyz")  
BNdrop.pack()  
MyButton = Button(root, text = "Show Selection", command = show).pack()   

root.mainloop()   

Above written little piece of code is giving the error written below:

'NoneType' object has no attribute '_root'  


What to import to correct this? Using python3 in windows not on unix.

What I have tried:

Tried to figure out what to import to remove this error message.
Posted
Updated 4-Nov-21 5:55am

1 solution

Assuming that you are using tkinter (which you omitted to mention), then you need the following:
Python
import tkinter as tk

root =  tk.Tk()

You will most likely need to use the tk. prefix on all your controls also, as shown below:
Python
import tkinter as tk

def show():
      myLabel = tk.Label(root, text = clicked.get()).pack()   

root = tk.Tk()
clicked = tk.StringVar()  
BNdrop = tk.OptionMenu(root, clicked, "abc", "xyz")  
BNdrop.pack()  
MyButton = tk.Button(root, text = "Show Selection", command = show).pack()   

root.mainloop()
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900