Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
69 views
in Technique[技术] by (71.8m points)

python - Why does my Tkinter calculator only output 0?

I have been trying to make a simple calculator in python using the Tkinter module, and all it does is display 0. Here is the code:

import tkinter as tk
window = tk.Tk()
calclabel = tk.Label(text = "This is a calculator.
Type yor numbers in the space given below and click on any on the operators to carry out the operation.")
calclabel.pack()
entry1 = tk.Entry()
entry1.pack()
entry2 = tk.Entry()
entry2.pack()
a = int()
b = int()
entry1.get = a
entry2.get = b
def addcommand () :
    addlabel = tk.Label(text = a+b)
    addlabel.pack()

def subcommand () :
    sublabel = tk.Label(text = a-b)
    sublabel.pack()

def multicommand () :
    multilabel = tk.Label(text = a*b)
    multilabel.pack()

def divicommand () :
    divilabel = tk.Label(text = a/b)
    divilabel.pack()

add = tk.Button(text = "Add",
                command = addcommand,
                master = window)
add.pack()

sub = tk.Button(text = "Substract",
                command = subcommand,
                master = window)
sub.pack()

mul = tk.Button(text = "Multiply",
                command = multicommand,
                master = window)
mul.pack()

div = tk.Button(text = "Divide",
                command = divicommand,
                master = window)
div.pack()

Any help would be greatly appreciated, because I think it is a logic error and I cannot figure it out.

question from:https://stackoverflow.com/questions/65885820/why-does-my-tkinter-calculator-only-output-0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems you are doing it the wrong way. Every time you perform any command, you should get the value from entry and then calculate. Like shown below

def addcommand () :
    a = int(entry1.get()) # get the value from entry1 and cast it to integer
    b = int(entry2.get()) # get the value from entry1 and cast it to integer
    addlabel = tk.Label(text = a+b) # do the calculation
    addlabel.pack()

You can use try except block during casting for handling exception.

Here is how you can do it

import tkinter as tk
window = tk.Tk()
calclabel = tk.Label(text = "This is a calculator.
Type yor numbers in the space given below and click on any on the operators to carry out the operation.")
calclabel.pack()
entry1 = tk.Entry()
entry1.pack()
entry2 = tk.Entry()
entry2.pack()
# a = int()
# b = int()
# entry1.get = a
# entry2.get = b
def addcommand () :
    a = int(entry1.get())
    b = int(entry2.get())
    addlabel = tk.Label(text = a+b)
    addlabel.pack()

def subcommand () :
    a = int(entry1.get())
    b = int(entry2.get())
    sublabel = tk.Label(text = a-b)
    sublabel.pack()

def multicommand () :
    a = int(entry1.get())
    b = int(entry2.get())
    multilabel = tk.Label(text = a*b)
    multilabel.pack()

def divicommand () :
    a = int(entry1.get())
    b = int(entry2.get())
    divilabel = tk.Label(text = a/b)
    divilabel.pack()

add = tk.Button(text = "Add",
                command = addcommand,
                master = window)
add.pack()

sub = tk.Button(text = "Substract",
                command = subcommand,
                master = window)
sub.pack()

mul = tk.Button(text = "Multiply",
                command = multicommand,
                master = window)
mul.pack()

div = tk.Button(text = "Divide",
                command = divicommand,
                master = window)
div.pack()

tk.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...