Python Forum
tkinter - "NameError: name 'frame' is not defined" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: tkinter - "NameError: name 'frame' is not defined" (/thread-19954.html)



tkinter - "NameError: name 'frame' is not defined" - Mocap - Jul-21-2019

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/46252200/nameerror-name-frame-is-not-defined-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



RE: tkinter - "NameError: name 'frame' is not defined" - Yoriz - Jul-21-2019

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.


RE: tkinter - "NameError: name 'frame' is not defined" - Mocap - Jul-21-2019

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


RE: tkinter - "NameError: name 'frame' is not defined" - Yoriz - Jul-21-2019

In the following link
https://python-forum.io/Thread-How-to-Execute-python-code
you are currently using 1) Interactive Prompt, use 2) IDE (Python script)