有 python 和 tkinter 问题的计算器

Calculator with python and tkinter issues

我可以通过相等函数添加 2 个数字,但我不知道如何根据您单击的按钮更改正在使用的运算符。

但是由于某些原因它不能添加小数。

还有一些我想知道的错误。

来自按钮的错误消息:

Assigning result of a function call, where the function has no return pylint(assignment-from-no-return)

Image

来自 from tkinter import * 的错误:

Unused import enum from wildcard importpylint(unused-wildcard-import)

calculator.py

from tkinter import *

root = Tk()
root.title("Calculator")

#Input box
e = Entry(root, width=70, borderwidth=5)
e.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

def btnclick(number):
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + str(number))

def c():
    e.delete(0, END)

def add():
    firstnum = e.get()
    global fnum
    fnum = int(firstnum)
    e.delete(0, END)

def sub():
    return

def div():
    return

def x():
    return

def eq():
    snum = e.get()
    e.delete(0, END)
    e.insert(0, fnum + int(snum))

def dec():
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + ".")


#Buttons
n1 = Button(root, text="1", padx=40, pady=20, command=lambda: btnclick(1)).grid(row=3, column=0)
n2 = Button(root, text="2", padx=40, pady=20, command=lambda: btnclick(2)).grid(row=3, column=1)
n3 = Button(root, text="3", padx=40, pady=20, command=lambda: btnclick(3)).grid(row=3, column=2)
n4 = Button(root, text="4", padx=40, pady=20, command=lambda: btnclick(4)).grid(row=2, column=0)
n5 = Button(root, text="5", padx=40, pady=20, command=lambda: btnclick(5)).grid(row=2, column=1)
n6 = Button(root, text="6", padx=40, pady=20, command=lambda: btnclick(6)).grid(row=2, column=2)
n7 = Button(root, text="7", padx=40, pady=20, command=lambda: btnclick(7)).grid(row=1, column=0)
n8 = Button(root, text="8", padx=40, pady=20, command=lambda: btnclick(8)).grid(row=1, column=1)
n9 = Button(root, text="9", padx=40, pady=20, command=lambda: btnclick(9)).grid(row=1, column=2)
n0 = Button(root, text="0", padx=40, pady=20, command=lambda: btnclick(0)).grid(row=4, column=0)
add = Button(root, text="+", padx=39, pady=20, command=add).grid(row=4, column=3)
c = Button(root, text="C", padx=40, pady=20, command=c).grid(row=4, column=2)
sub = Button(root, text="-", padx=39, pady=20, command=sub).grid(row=3, column=3)
x = Button(root, text="x", padx=40, pady=20, command=x).grid(row=2, column=3)
div = Button(root, text="/", padx=39, pady=20, command=div).grid(row=1, column=3)
eq = Button(root, text="=", padx=220, pady=20, command=eq).grid(row=5, column=0, columnspan=4)
dec = Button(root, text=".", padx=39, pady=20, command=dec).grid(row=4, column=1)

root.mainloop()

But it cant add decimal numbers for some reason.

是因为你这样做了:

fnum = int(firstnum)

你告诉它 firstnum 是一个整数。尝试:

fnum = float(firstnum)

but i dont know how to change what operator is being used based on what button you clicked

有很多方法可以为这个设置皮肤,但一个简单的方法是声明一个全局变量 operator 并为其分配一个代表所单击操作的值。

例如:

def x():
    operator = 'multiplication'
    return

然后在你的 equal 函数中:

def eq():
    snum = float(e.get())
    e.delete(0, END)
    result = 0
    if operator == "addition":
        result = fnum + snum
    elif operator == "multiplication":
        result = fnum * snum
    # .... etc, etc.... 

    e.insert(0, result)