0

This is the code I have created and am trying to run:

import tkinter as tk


def ok(val):
    print("Value is: ", val)

def say_hi(self):
    print("hi there, everyone!")

class Application(tk.Frame):

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        master.title("Hi There")
        master.geometry("400x400")
        self.createWidgets(master)

    def createWidgets(self, master=None):
        var = str()

        self.select = tk.OptionMenu(master, var, "one", "two","three", command=ok).grid(column=1, row=1)

        self.QUIT = tk.Button(master, text="QUIT", fg="red", command=root.destroy).grid(column=2, row=1)
        print ("HI")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

But I am receiving the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Python34\lib\tkinter\__init__.py", line 3308, in __call__
    self.__var.set(self.__value)
AttributeError: 'str' object has no attribute 'set'

I tried messing with some of the variables and using different methods to get the menu to work, but nothing seemed to get rid of the error.

Any idea on how to fix the error?

0

1 Answer 1

7

Use a StringVar not a str

  def createWidgets(self, master=None):
        var = tk.StringVar()

A python str has no set method or attribute, a StringVar is specific to tkinter and what you are meant to be using.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! I originally had it as a StringVar but it would not work. I got on today to try it and it worked perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.