I have the following code:
from tkinter import *
class Button:
def __init__(self, master):
frame = Frame( master )
frame.pack()
self.printButton = Button(frame, text = "Print Message", command=self.printMessage)
self.printButton.pack(side = LEFT)
self.quitButton = Button(frame, text = "Quit", command = frame.quit)
self.quitButton.pack(side = LEFT)
def printMessage(self):
print(" WORKING!! " )
root = Tk()
b = Button(root)
root.mainloop()
Which does not seem to be wrong in anyway... But when I run it, terminal says:
Traceback (most recent call last):
File "class.py", line 23, in <module>
b = Button(root)
File "class.py", line 10, in __init__
self.printButton = Button(frame, text = "Print Message", command=self.printMessage)
TypeError: __init__() got an unexpected keyword argument 'command'
I wrote all these codes according to a tkinter tutorial. And in the tutorial, the code works well. Any help would be appreciated. Thanks in advance!
Buttonclass. Try renaming it toCustomButton.from tkinter import *but you have now discovered why this is a bad idea.from tkinter import *you import over 170 names (in Python 2, I'm not sure of the exact figure in Python 3). Not only is that annoying because of the potential clash with names you define yourself, but there's an even bigger problem. Imagine what happens if you then doimport *with another big module. Names in the new module can clobber the names from the Tkinter module, which will leading to weird bugs. Hopefully, the code will just fail to run, giving a helpful error message. But it's possible that it will run, but just not do what you expect it to.