Python Forum

Full Version: tkinter - "NameError: name 'frame' is not defined"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to create a class of buttons and bind it to tkinter but it keeps telling me "NameError: name 'frame' is not defined."

I tried this solution here but it's not working - https://stackoverflow.com/questions/4625...ned-python

Here's what it looks like:

>>> class AccountingButtons:

	def __init__(self, master):
		frame = Frame(master)
		frame.pack()

		
>>> self.incomeopButton = Button(frame, text="Display Income From Operations", command=self.income_from_operations)
Traceback (most recent call last):
  File "<pyshell#76>", line 1, in <module>
    self.incomeopButton = Button(frame, text="Display Income From Operations", command=self.income_from_operations)
NameError: name 'frame' is not defined
>>> from tkinter import Frame
>>> from tkinter import Text
>>> from tkinter import Label
>>> class AccountingButtons:

	def __init__(self, master):
		frame = Frame(master)
		frame.pack()

		
>>> self.incomeopButton = Button(frame, text="Display Income From Operations", command=self.income_from_operations)
Traceback (most recent call last):
  File "<pyshell#86>", line 1, in <module>
    self.incomeopButton = Button(frame, text="Display Income From Operations", command=self.income_from_operations)
NameError: name 'frame' is not defined
frame is not defined because
self.incomeopButton = Button(frame, text="Display Income From Operations", command=self.income_from_operations)
has not been added as part of the __init__ method of AccountingButtons, its in a separate command line.
create this code as a python file instead.
(Jul-21-2019, 07:09 PM)Yoriz Wrote: [ -> ]frame is not defined because
self.incomeopButton = Button(frame, text="Display Income From Operations", command=self.income_from_operations)
has not been added as part of the __init__ method of AccountingButtons, its in a separate command line.
create this code as a python file instead.

Thank you for the help. I apologize for not posting properly and will do so in the future.

In the meantime though, can you link me to a description on how to do what you're describing? I want to make sure I don't make this errors or similar ones in the future.
In the following link
https://python-forum.io/Thread-How-to-Ex...ython-code
you are currently using 1) Interactive Prompt, use 2) IDE (Python script)