Python Forum
[Tkinter] Im embarresed to ask, but Im stuck
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Im embarresed to ask, but Im stuck
#1
Im getting a NameError: name ' Application ' is not defined error. I want to create a simple GUI interface using tkinter for the first time. I would appreciate some insight here. Heres the code:

from tkinter import*

class Application(Frame):
def say_Yeh(self):
print("Yeh! Your button just did something!")

def createWidgets(self):
self.QUITBUTTON= Button(self)
self.QUITBUTTON['text']= 'QUIT BUTTON'
self.QUITBUTTON['command']= self.quit
self.QUITBUTTON['fg']= 'red'

self.QUITBUTTON.pack({'side' : 'left'})

self.do_something= Button(self)
self.do_something['text']= ' CLICK TO DO SOMETHING BUTTON',
self.do_something['command']= self.say_Yeh

self.do_something.pack({'side': 'left'})

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

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


Here's the error:

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>>
============ RESTART: /Users/amyheu-wong/Documents/FrameButton.py ============
Traceback (most recent call last):
File "/Users/amyheu-wong/Documents/FrameButton.py", line 3, in <module>
class Application(tk.Frame):
File "/Users/amyheu-wong/Documents/FrameButton.py", line 27, in Application
app= Application(master=root)
NameError: name 'Application' is not defined
>>>
Reply
#2
I tried running the code on my computer and it didn't give me any errors...
Reply
#3
(Oct-29-2017, 01:00 PM)Lux Wrote: I tried running the code on my computer and it didn't give me any errors...

I guess OP has wrong indentation problem, but without using BBcode and thus preserving the original indentation it;s not possible to be certain.
Reply
#4
Interesting, hmm what can it be. I didn't think anything was wrong with the code. I tried this on ubuntu 14.04, Windows 10 and my mac and couldn't run it without an error. So you see the GUI interface after running the code?

Sorry, I need to learn how to post in BBcode. Can you point me in the write direction? I'll try an figure it out on my own for now..

BTW: thank you for looking into it. I appreciate it as I am trying to learn without being called dumb..

THANK YOU buron and Lux! It was an indentation issue. I'll be sure to post properly on my next post. Thank you again, you guys made my evening :)
Reply
#5
click Help/ Rules
then BBCODE
Reply
#6
hi, try with this little fix

root= Tk()
app1 = Application(parent=root) # parent to root
app= Application(master=root)   # edit it
app.mainloop()
root.destroy()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020