【问题标题】:Why does my Tkinter calculator only output 0?为什么我的 Tkinter 计算器只输出 0?
【发布时间】:2021-04-29 07:41:30
【问题描述】:

我一直在尝试使用 Tkinter 模块在 python 中制作一个简单的计算器,它所做的只是显示 0。这是代码:

import tkinter as tk
window = tk.Tk()
calclabel = tk.Label(text = "This is a calculator.\nType 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()

任何帮助将不胜感激,因为我认为这是一个逻辑错误,我无法弄清楚。

【问题讨论】:

    标签: python tkinter calculator


    【解决方案1】:

    看来你做错了。 每次执行任何command,都应该从entry获取值,然后计算。 如下图所示

    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()
    

    您可以在转换过程中使用try except 块来处理exception

    你可以这样做

    import tkinter as tk
    window = tk.Tk()
    calclabel = tk.Label(text = "This is a calculator.\nType 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()
    

    【讨论】:

    • 也许更多的 cmets 而不是“看起来你做错了”可能会有所帮助 - 你的答案有什么变化?
    • 感谢您的建议 :)
    • 每次调用这些命令之一时,您仍然会创建一个新标签。除非 OP 明确声明他希望保留所有过去计算的日志,否则最好建议您应该拥有第三个 tkinter 对象,您每次都使用计算结果进行更新。
    • 非常感谢!现在完美运行!
    • @ArjunSharma 不要忘记接受答案 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    相关资源
    最近更新 更多