Home > other >  "AttributeError: 'function' object has no attribute set" tkinter
"AttributeError: 'function' object has no attribute set" tkinter

Time:10-16

Please comment on this question if you think it is needs improvements, or needs to be deleted. Googled around for a few hours, and I can't find an answer. If there is one, please point me to that website and I will delete this question and use that.
Anyway, I am getting an error that says this when I am trying to use an OptionMenu in tkinter. This is the error:

AttributeError: 'function' object has no attribute 'set'

The code:

from tkinter import *
w = Tk()
w.geometry("250x250")
w.title("OptionMenu Testing")
def DoNothing():
    pass
options = ["Option1", "Option2", "Option3"]
DropdownMenuVar = StringVar()
DropdownMenuVar.set("Option1")
DropdownMenu = OptionMenu(w, DoNothing, *options)
DropdownMenu.place(x=175, y=200)

I can see the options, but when I click on one, it does that! This code is just a test script for another script that involves OptionMenus.

EDIT: Just removed the function parameter, now I am getting this:
AttributeError: 'str' object has no attribute 'set'.

CodePudding user response:

This line:

DropdownMenu = OptionMenu(w, DoNothing, *options)

Should be like this:

DropdownMenu = OptionMenu(w, DropdownMenuVar, *options)

Unrelated, but I highly recommend you use PEP8 style names. It makes your code much easier to read.

  • Related