所以我正在尝试使用tkinter创建一个基本的计算器。我想将阶乘函数添加到我的计算器中,但我不知道如何。这是我的代码:
from tkinter import *
from tkinter import messagebox
from math import *
from math import factorial as fact
class Aplication(Frame):
def __init__(self, master, *args, **kwargs):
Frame.__init__(self, master, *args, **kwargs)
self.createWidgets()
def replaceText(self, text):
self.display.delete(0, END)
self.display.insert(0, text)
def clcExp(self):
self.expression = self.display.get()
self.expression = self.expression.replace("×", "*")
self.expression = self.expression.replace("÷", "/")
self.expression = self.expression.replace("%", "/ 100")
self.expression = self.expression.replace("!", "fact")
self.expression = self.expression.replace("mod", "%")
self.expression = self.expression.replace("√", "** 0.5")
self.expression = self.expression.replace("²", "** 2")
self.expression = self.expression.replace("³", "** 3")
self.expression = self.expression.replace(",", ".")
try:
self.result = eval(self.expression)
self.replaceText(self.result)
except:
messagebox.showinfo("Invalid Input", "ERROR")
def appendToDisplay(self, text):
self.entryText = self.display.get()
self.textLength = len(self.entryText)
if self.entryText == "0":
self.replaceText(text)
else:
self.display.insert(self.textLength, text)
def clearText(self):
self.replaceText("0")
def createWidgets(self):
self.display = Entry(self, font = ("Times", 16), borderwidth = 1, relief = RAISED, justify = RIGHT)
self.display.insert(0, "0")
self.display.grid(row = 0, column = 0, columnspan = 5)
self.pctBtn = Button(self, font = ("Times", 12), text = "%", borderwidth = 1, command = lambda: self.appendToDisplay("%"))
self.pctBtn.grid(row = 1, column = 0, sticky ='NWNESWSE')
self.rootBtn = Button(self, font = ("Times", 12), text = "√", borderwidth = 1, command = lambda: self.appendToDisplay("√"))
self.rootBtn.grid(row = 2, column = 0, sticky ='NWNESWSE')
self.sqrBtn = Button(self, font = ("Times", 12), text = "x²", borderwidth = 1, command = lambda: self.appendToDisplay("²"))
self.sqrBtn.grid(row = 3, column = 0, sticky ='NWNESWSE')
self.cubeBtn = Button(self, font = ("Times", 12), text = "x³", borderwidth = 1, command = lambda: self.appendToDisplay("³"))
self.cubeBtn.grid(row = 4, column = 0, sticky ='NWNESWSE')
self.modBtn = Button(self, font = ("Times", 12), text = "mod", borderwidth = 1, command = lambda: self.appendToDisplay(" mod "))
self.modBtn.grid(row = 5, column = 0, sticky ='NWNESWSE')
self.clearBtn = Button(self, font = ("Times", 12), text = "CLEAR", borderwidth = 1, command = lambda: self.clearText())
self.clearBtn.grid(row = 1, column = 1, columnspan = 3, sticky ='NWNESWSE')
self.sevenBtn = Button(self, font = ("Times", 12), text = "7", borderwidth = 1, command = lambda: self.appendToDisplay("7"))
self.sevenBtn.grid(row = 2, column = 1, sticky ='NWNESWSE')
self.fourBtn = Button(self, font = ("Times", 12), text = "4", borderwidth = 1, command = lambda: self.appendToDisplay("4"))
self.fourBtn.grid(row = 3, column = 1, sticky ='NWNESWSE')
self.oneBtn = Button(self, font = ("Times", 12), text = "1", borderwidth = 1, command = lambda: self.appendToDisplay("1"))
self.oneBtn.grid(row = 4, column = 1, sticky ='NWNESWSE')
self.fact = Button(self, font = ("Times", 12), text = "n!", borderwidth = 1, command = lambda: self.appendToDisplay("!"))
self.fact.grid(row = 5, column = 1, sticky ='NWNESWSE')
self.eightBtn = Button(self, font = ("Times", 12), text = "8", borderwidth = 1, command = lambda: self.appendToDisplay("8"))
self.eightBtn.grid(row = 2, column = 2, sticky ='NWNESWSE')
self.fiveBtn = Button(self, font = ("Times", 12), text = "5", borderwidth = 1, command = lambda: self.appendToDisplay("5"))
self.fiveBtn.grid(row = 3, column = 2, sticky ='NWNESWSE')
self.twoBtn = Button(self, font = ("Times", 12), text = "2", borderwidth = 1, command = lambda: self.appendToDisplay("2"))
self.twoBtn.grid(row = 4, column = 2, sticky ='NWNESWSE')
self.zeroBtn = Button(self, font = ("Times", 12), text = "0", borderwidth = 1, command = lambda: self.appendToDisplay("0"))
self.zeroBtn.grid(row = 5, column = 2, sticky ='NWNESWSE')
self.nineBtn = Button(self, font = ("Times", 12), text = "9", borderwidth = 1, command = lambda: self.appendToDisplay("9"))
self.nineBtn.grid(row = 2, column = 3, sticky ='NWNESWSE')
self.sixBtn = Button(self, font = ("Times", 12), text = "6", borderwidth = 1, command = lambda: self.appendToDisplay("6"))
self.sixBtn.grid(row = 3, column = 3, sticky ='NWNESWSE')
self.threeBtn = Button(self, font = ("Times", 12), text = "3", borderwidth = 1, command = lambda: self.appendToDisplay("3"))
self.threeBtn.grid(row = 4, column = 3, sticky ='NWNESWSE')
self.commaBtn = Button(self, font = ("Times", 12), text = ",", borderwidth = 1, command = lambda: self.appendToDisplay(","))
self.commaBtn.grid(row = 5, column = 3, sticky ='NWNESWSE')
self.divisionBtn = Button(self, font = ("Times", 12), text = "÷", borderwidth = 1, command = lambda: self.appendToDisplay("÷"))
self.divisionBtn.grid(row = 1, column = 4, sticky ='NWNESWSE')
self.timesBtn = Button(self, font = ("Times", 12), text = "×", borderwidth = 1, command = lambda: self.appendToDisplay("×"))
self.timesBtn.grid(row = 2, column = 4, sticky ='NWNESWSE')
self.minusBtn = Button(self, font = ("Times", 12), text = "-", borderwidth = 1, command = lambda: self.appendToDisplay("-"))
self.minusBtn.grid(row = 3, column = 4, sticky ='NWNESWSE')
self.plusBtn = Button(self, font = ("Times", 12), text = "+", borderwidth = 1, command = lambda: self.appendToDisplay("+"))
self.plusBtn.grid(row = 4, column = 4, sticky ='NWNESWSE')
self.equalBtn = Button(self, font = ("Times", 12), text = "=", borderwidth = 1, command = lambda: self.clcExp())
self.equalBtn.grid(row = 5, column = 4, sticky ='NWNESWSE')
calculator = Tk()
calculator.title("Calculator")
calculator.resizable(0,0)
app = Aplication(calculator).grid()
calculator.mainloop()
我尝试将factorial导入为事实,并使用
替换self.expression = self.expression.replace("!", "fact")
但是当我尝试放5个!我收到了我在消息框中写的“无效输入”错误。我测试了导入平方根并使用sqrt而不是** 0.5的相同内容。我如何添加阶乘?我为格式错误道歉,这是我在这里的第一篇文章。
答案 0 :(得分:-1)
你可以"猴子补丁" float类有factorial()
方法,然后5!
将成为5.factorial()
。