2

here is a snippet of code from my first attempt at a GUI.

When the 'Submit' button is pushed, I would like to display a label and OptionMenu below the button. I have got the label working, and searched around for a solution, but I just cannot get the OptionMenu working. Any help would be much appreciated!

The error I receive is:

self._root = master._root()
AttributeError: 'MyApp' object has no attribute '_root'

My code:

from tkinter import *


class MyApp:

    def __init__(self, master):
        self.master = master
        master.title("My Application")
        master.minsize(width=800, height=800)
        master.maxsize(width=800, height=800)

        ##### Setup & Display the Labels
        self.headerLabel = Label(master, text="Label")
        self.headerLabel.grid(row=0, sticky=E)

        self.headerEntry = Label(master, text="Enter")
        self.headerEntry.grid(row=0, column=1, sticky=N)

        ##### Submit Button
        self.getButton = Button(root, text="Submit", command=self.dostuff_button)
        self.getButton.grid(row=7, column=1)

    def dostuff_button(self):
        self.inputfumelevel()

    def inputfumelevel(self):
        self.inpfumeLabel = Label(text="Input Your Fume Level")
        self.inpfumeLabel.grid(row=12, column=0)

        self.fumeEntry = StringVar(self)
        self.fumeEntry.set("Select Fume")
        self.fumeEntryOM = OptionMenu(self, self.fumeEntryOM, '1', '2', '3')
        self.fumeEntryOM.grid(row=12, column=1)


root = Tk()
my_gui = MyApp(root)
root.mainloop()
3
  • There are some formatting issues with your pasted code. You might want to fix them so that we can be more helpful. Specifically, the first two lines and the very last line seem to need 4 spaces preceding them, and also it isn't clear whether dostuff_button and inputfumelevel are supposed to be methods or functions. Commented May 5, 2015 at 6:31
  • Woops sorry about that. I think I have fixed the formatting issues. From my incredibly limited programming experience, dostuff_button and inputfumelevel are both meant to be functions. Commented May 5, 2015 at 6:45
  • Can you add the Traceback? I'm believing that the error your receiving is coming from an internal file. Commented Jul 26, 2015 at 0:15

2 Answers 2

3

This is where the problems are.

self.fumeEntry = StringVar(self)

self.fumeEntryOM = OptionMenu(self, self.fumeEntryOM, '1', '2', '3')

The master or parent of these widgets must be a Tkinter widget. self (referring to MyApp class) is just a class and not a Tkinter widget.

You have two options available to you.

  1. Make MyApp a subclass of the Tk() widget

    class MyApp(Tk):
    # extra code goes here
    
  2. Make the StringVar and OptionMenu slaves of self.master.

    self.fumeEntry = StringVar(self.master)
    # extra code
    self.fumeEntryOM = OptionMenu(self.master, self.fumeEntry, "1", "2", "3")
    
Sign up to request clarification or add additional context in comments.

Comments

0

Your option menu isn't being initialised with the variable. You have:

self.fumeEntryOM = OptionMenu(self, self.fumeEntryOM, '1', '2', '3')

Should be:

self.fumeEntryOM = OptionMenu(self, self.fumeEntry, '1', '2', '3')

2 Comments

HI samort7, thanks for pointing that out. Unfortunately I am still receiving the same error, even though you have correctly pointed out an issue with my code.
No problem. Check out this page and how they setup a class: java2s.com/Code/Python/GUI-Tk/DefineGUIinaclass.htm You're mixing 'master' and 'root'. If my memory serves me correctly those are meaningless keywords, however they should be consistently the same in your program.

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.