Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Luke Ghislain
3,477 PointsText Variable in Tkinter Python 2.7 class
I was trying to use a String Variable in a Class when I get this error: AttributeError: 'NoneType' object has no attribute '_root'. So I put the root above the String Variable but now the window does not show up on screen.
I tried it on a separate program and it works! Can you please tell me what's wrong.
Working Code:
import Tkinter
from Tkinter import StringVar
class Input:
def __init__(self, master):
self.master = master
self.mainframe = Tkinter.Frame(self.master, bg = "White")
self.mainframe.pack(fill = Tkinter.BOTH, expand = True)
self.ehello()
self.Entry()
def ehello(self):
etext = e.get()
elabel = Tkinter.Label(self.mainframe, text = etext)
elabel.pack()
return
root = Tkinter.Tk()
global root
e = StringVar()
global e
def Entry(self):
eEntry = Tkinter.Entry(self.mainframe, textvariable = e)
eEntry.pack()
ebutton = Tkinter.Button(self.mainframe,
text = 'OK',
command = self.ehello)
ebutton.pack()
if __name__ == '__main__':
# root = Tkinter.Tk()
Input(root)
root.mainloop()
Error Code: import Tkinter from Tkinter import StringVar
class the_list:
def __init__(self, master):
self.master = master
self.mainframe = Tkinter.Frame(self.master, bg = "White")
self.mainframe.pack(fill = Tkinter.BOTH, expand = True)
# self.build_grid()
# self.build_banner()
# self.add_to_list()
self.Entry()
self.ehello()
def ehello(self):
etext = e.get()
elabel = Tkinter.Label(self.mainframe, text = etext)
elabel.grid(row = 6, column = 6)
return
root = Tkinter.Tk()
global root
e = StringVar()
global e
def Entry(self):
eEntry = Tkinter.Entry(self.mainframe, textvariable = e)
eEntry.grid(row = 6, column = 1)
ebutton = Tkinter.Button(self.mainframe,
text = 'OK',
command = self.ehello)
ebutton.pack()
if __name__ == '__main__':
# root = Tkinter.Tk()
the_list(root)
root.mainloop()
[MOD: edited code block - srh]