from tkinter import*
class ListButtons(object):
def _init_(self,master):
frame = frame(master)
frame.pack()
self.printButton = Button(frame,text = 'print button',command = printMessage)
self.printButton.pack( side = LEFT)
self.quitButton =Button(frame,text = 'exit',command = frame.quit)
self.printButton.pack( side = LEFT)
def printMessage(self):
print ('this actually worked')
root = Tk()
b = ListButtons(root) #I get error object does not take any parameter#when I remove the root I get attribute error
root.mainloop()
Asked
Active
Viewed 39 times
0
1 Answers
1
You are missing the double underscore for the constructor of your class, with your code the compiler thinks it's just any ordinary method (no name mangling). See this post for more info:
What is the meaning of a single- and a double-underscore before an object name?
Also you can leave out the superclass since object
is inherited by default. Frame needs to be capitalized and you need to self reference the the print message otherwise you will get an error. This should work:
from Tkinter import*
class ListButtons:
def __init__(self,master):
frame = Frame(master)
frame.pack()
self.printButton = Button(frame,text = 'print button',command =
self.printMessage)
self.printButton.pack( side = LEFT)
self.quitButton =Button(frame,text = 'exit',command = frame.quit)
self.printButton.pack( side = LEFT)
def printMessage(self):
print ('this actually worked')
root = Tk()
b = ListButtons(root)
root.mainloop()