2

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!

5
  • 1
    You are just creating another instance of your own Button class. Try renaming it to CustomButton. Commented Mar 6, 2016 at 14:01
  • 2
    Lots of Tkinter example code uses from tkinter import * but you have now discovered why this is a bad idea. Commented Mar 6, 2016 at 14:08
  • @PM2Ring , Whenever I learn some basics like this one you mentioned, my life makes even more sense :P . Thanks a lot for the advice! Commented Mar 7, 2016 at 10:19
  • My pleasure! FWIW, when you do 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 do import * 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. Commented Mar 7, 2016 at 10:32
  • @PM2RING That sounds like, executing a secret combination to create an evil artificial intelligence :P . I will never use " import * " again... or, dont you think it worths trying?? oh okay nevermind, to much sci-fic :) . Commented Mar 10, 2016 at 20:07

2 Answers 2

4

Tkinter already has a Buttonclass and when you create your class you now have overwritten the tkinter class named Button. So, when you try to create a tkinter button like this:

self.printButton = Button(frame, text = "Print Message", command=self.printMessage)

You are now referencing your button because you overwrote the tkinter button previously. And since your button only takes one argument and you give it three, It will throw you an error. The way to fix this would be to change your import line to this:

import tkinter as tk

And then reference any tkinter functions with tk.*. For example:

root = Tk()

would become:

root = tk.Tk()

Then your button would be referenced by Button while the tkinter button would be referenced by tk.Button. This way you could easily distingush between the two. However you could also just call your button something like myButton which would also fix the problem.

Sign up to request clarification or add additional context in comments.

4 Comments

"you now have two classes named Button". I get what you mean, but that's not strictly true since you can't have two things with the same name in the same scope.
Yes, I see what you mean. How would you word it instead?
Maybe something like: "Two different things in the same scope can't have the same name, so when you create your Button class it replaces the previous Button class defined by Tkinter". And maybe give an example illustrating the same thing with simple variables, eg a='one'; a=1
Thanks so very much everybody! That s a very clear answer.
-1
class Personne:
    def __int__(self, nom: str, age: int, genre: bool):
        self.nom = nom
        self.age = age
        self.genre = genre
        print("contructeur personne" + self.nom)

    def SePresenter(self):
        # bonjour je m'appelle jean, j'ai 30ans
        # je suis majeur
        if self.genre:
            print("bonjour, je m'appelle" + self.nom + "j'ai" + str(self.age) + "ans")
            print("Genre : Masculin")
            if self.EstMajeur():
                print('je suis majeur')
            else:
                print("je suis mineur")
            print()
        else:
            print("Bonjour, je m'appelle" + self.nom + "j'ai" + str(self.age) + "ans")
            print("Genre : Feminin")
            if self.EstMajeur():
                print("je suis majeure")
            else:
                print("je suis mineure")
            print()

    def EstMajeur(self):
        return self.age >= 18

personne1 = Personne('jean', 20 , True)

3 Comments

the problem of my code please!
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.