pythonpython-3.xtkintertkinter.optionmenu

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


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'.


Solution

  • 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.