0

I was trying to create a code to create a GUI Calculator with buttons and I tried looking up Tkinter help, but I was not able to get help. How do I incorporate the commented out section at the bottom to display the value of the numbers shown? Please feel free to add suggestions to my code.

EDIT: I fixed the code as @abarnert suggested but when I run the code, it always writes Invalid Operation. I do not know how to fix this. Does anyone have a better idea to fix my code? I just need to get the operation to work. My edited code is below.

from math import sqrt
from tkinter import *
window = Tk()
window.title("Welcome to Calculator ")
window.configure(background = "white")
Label (window, text = "Calculator", bg = "white") .grid(row = 0, column = 
0, sticky = N)



#click function
def click():
    n = n_textentry.get()
    m = m_textentry.get()
    operation = operation_textentry.get()
    if operation == 1:
        print(n + m)

    elif operation == 2:
        print(n - m)

    elif operation == 3:
        print(n * m)

    elif operation == 4:
        print(n / m)

    elif operation == 5:
        print(n ** m)

    elif operation == 6:
        print(sqrt(n))

    else:
        print("Invalid Operation ")




#to create the box for the first number and store it
Label (window, text = "Enter the first number", bg = "white") .grid(row = 
1, column = 0, sticky = N)
n_textentry = Entry(window, width = 10, bg = "white")
n_textentry.grid(row = 2, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 3, 
column = 0, sticky = N)

#to create the box for the second number
Label (window, text = "Enter the second number", bg = "white") .grid(row = 
5, column = 0, sticky = N)
m_textentry = Entry(window, width = 10, bg = "white")
m_textentry.grid(row = 6, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 7, 
column = 0, sticky = N)

#to show list of options
Label (window, text = '''               Enter 1 for addition
                    Enter 2 for subtraction
                         Enter 3 for multiplication
              Enter 4 for division
                        Enter 5 for exponentiation

Enter 6 for square root *This will only work for 1st choice*''', bg = 
"white") .grid(row = 9, column = 0, sticky = W)

operation_textentry = Entry(window, width = 10, bg = "white")
operation_textentry.grid(row = 10, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 11, 
column = 0, sticky = N)

1 Answer 1

3

In a GUI program, once the GUI is up and running, you can only run your code in the callbacks to event handlers. This is called event-driven programming, and it can take a while to get used to at first.

For example, when the user clicks that first Submit button, it calls your function click. Inside that function, you can do whatever's appropriate there.

You've made all three buttons call the same click function. That doesn't make too much sense here.

In fact, why do you even want three separate buttons? Think of a typical form in a typical GUI—there's a bunch of Entry fields, and then a single "Submit" button that the user clicks after filling out all of the fields. So, let's just scrap the first two buttons and only have one at the end.

Now, inside that click function, you can "do whatever's appropriate". But what's appropriate here?

The first thing you need to do is get the values out of the three Entry boxes. But you tried to store all three of them in the same variable, so you can't access all three of them, only the last one. So, instead of doing this three times:

textentry = Entry(window, width = 10, bg = "white")

… give them all different names:

n_entry = Entry(window, width = 10, bg = "white")
# ...
m_entry = Entry(window, width = 10, bg = "white")
# ...
operation_entry = Entry(window, width = 10, bg = "white")

And now, your click function can get all three values:

def click():
    n = n_entry.get()
    m = m_entry.get()
    operation = operation_entry.get()

Of course these are going to be strings—exactly like what you get back from calling input() in a command-line app. So, you have to do the same thing here—call int or float on them, handle errors, etc.

But, once you've done that, the rest of click can be your existing commented-out code. (Except that you have to get rid of those break statements—there's no loop to break out of here.)

    if operation == 1:
        print(n + m)
    elif operation == 2:
        print(n - m)
    # etc.

However, you might want to change those print calls to instead display the result in the GUI. For example, you might have an empty Label named results_label, and config its text to be str(n+m), etc.

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

5 Comments

Good answer, I'll just add that we call the way GUI's are run "event driven programming", as opposed to sequential programming which most beginner start with.
@Novel Nice link; I'll add that to the answer—and remember to look at Wikipedia next time. (There are so many questions from users who don't get the change in abstraction when writing their first event-driven GUI or network server, or, less commonly, frameloop-driven game or audio program…)
Where would I place the empty label though?
@MonishShah You mean where in the GUI? That's really a graphical design question, not a programming question. But given the kind of form you're designing, I'd go with either to the right or the bottom of the form—past the "Submit" button.
No what I mean is where would I place the result to work because I have trouble getting the result to work. I created a better post where it talks about my problem in more depth. stackoverflow.com/questions/51141046/…

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.