0

I was really curious why I cannot get my add_button to work, as the window fails to come up when creating it.

from tkinter import *
class Calculator:

#-------------------------------------------------               
    def __init__(self, master):

        self.master = master 

        master.title("Calculator")

        self.close_button = Button(master, text = "Close", command = master.destroy)        

        Label(master, text = "First Digit").grid(row = 0)
        Label(master, text = "Second Digit").grid(row = 1)

        self.input1 = 0
        self.input2 = 0

        input1 = Entry(master)
        input2 = Entry(master)

        input1.grid(row = 0, column = 1)
        input2.grid(row = 1, column = 1)


        self.close_button.grid(row = 2, column = 0)

        self.add_buton = Button(master, text = "Add", command = self.add())
        self.add_button.grid(row = 2, column = 1)                              

        master.configure(background = 'grey')

        return 

#-------------------------------------------------

    def add(self):
            return self.input1.get() + self.input2.get()   

#-------------------------------------------------



#-------------------------------------------------
root = Tk()
calc = Calculator(root)
root.mainloop()
#-------------------------------------------------
1
  • Try command = self.add instead of command = self.add(). You are calling the callback method and using it's result as the actual callback. Also, you are confusing self.input1 and input1, and misspelled add_button once. Also, where does the result from the callback go? Commented Jan 21, 2019 at 16:28

1 Answer 1

1

Welcome to Stack!

I've looked through you code I've been able to do what you are asking. There were a few errors within your code:

a) you had self.add_buton and self.add_button which caused an error.

b) self.input1 = 0 and self.input2 = 0 are not required.

c) You were calling self.add() as the command and you should be calling self.add. When calling it as a command you do not need ()

d)input1 = Entry(master) should be self.input1 = tk.Entry(master)

e) You should convert your input values into int or float as otherwise it will just one value onto the end of the other. (Eg, 1 + 5 = 15 whereas int(1) + int(5) = 6

Here is your code with the entry boxes working as they should. I have import tkinter as tk hence why it is tk.Entry

from tkinter import *
import tkinter as tk
class Calculator:

#-------------------------------------------------               
    def __init__(self, master):

        self.master = master 

        master.title("Calculator")

        self.close_button = Button(master, text = "Close", command = master.destroy)        

        Label(master, text = "First Digit").grid(row = 0)
        Label(master, text = "Second Digit").grid(row = 1)

        self.input1 = tk.Entry(bd=5, width=35, background='gray35', foreground='snow')
        self.input2 = tk.Entry(bd=5, width=35, background='gray35', foreground='snow')

        self.input1.grid(row = 0, column = 1)
        self.input2.grid(row = 1, column = 1)


        self.close_button.grid(row = 2, column = 0)

        self.add_button = tk.Button(master, text = "Add", command = self.add)
        self.add_button.grid(row = 2, column = 1)                              

        master.configure(background = 'grey')

        return 

#-------------------------------------------------

    def add(self):
        val = self.input1.get()
        print(val)

#-------------------------------------------------



#-------------------------------------------------
root = Tk()
calc = Calculator(root)
root.mainloop()

This should now work how you wanted it too. The variables within the entry can be changed to suit. You were correct in calling the value of the entry with self.input1.get().

Hope this has helped.

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

2 Comments

One more thing: the input values should probably be converted to int or float, otherwise + will just concatenate the strings. (But you removed that part from your answer anyway)
@tobias_k Yes, I forget about that, but will add it to my answer. Thanks for reminding me.

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.