计算器 - 电源按钮

Calculator - Power Button

我一直在尝试用 tkinter 编写一个计算器,到目前为止,我已经完成了所有四种基本运算,加法、减法、乘法和除法。我还为所有数字制作了一个清晰的按钮和按钮。现在我想做一个 "power" 按钮,但我不知道怎么做。当我把它放入计算器时,答案不是正确答案。有谁知道如何让电源按钮工作而不影响其他按钮吗?

代码在下面,这样你就可以看到我一直在做什么。

 #calculator with tkinter
import sys
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog

a = Tk()

frame = Frame(a)
frame.pack()

a.title('Calculator')

def clear():
    mbox = textDisplay.delete(len(textDisplay.get())-1, END)
    return
def set_text(text):
    textDisplay.insert(END, text)
    return

def clear_all():
    textDisplay.delete(0, END)
    return

def equals():
    try:
        result = eval(textDisplay.get())
    except:
        messagebox.showerror(message = 'Invalid Answer')
    clear_all()
    set_text(result)





box = StringVar()

topframe = Frame(a)
topframe.pack(side = TOP)

textDisplay = Entry(frame, textvariable = box, bd = 20, insertwidth = 1, font = 30)
textDisplay.pack(side = TOP)

button1 = Button(topframe, padx = 16, pady = 16, bd = 8, text = '1', command = lambda:set_text('1'))
button1.pack(side = LEFT)

button2 = Button(topframe, padx = 16, pady = 16, bd = 8, text = '2', command = lambda:set_text('2'))
button2.pack(side = LEFT)

button3 = Button(topframe, padx = 16, pady = 16, bd = 8, text = '3', command = lambda:set_text('3'))
button3.pack(side = LEFT)

plus = Button(topframe, padx = 16, pady = 16, bd = 8, text = '+', command = lambda:set_text('+'))
plus.pack(side = LEFT)

middleframe = Frame(a)
middleframe.pack(side = TOP)

button4 = Button(middleframe, padx = 16, pady = 16, bd = 8, text = '4', command = lambda:set_text('4'))
button4.pack(side = LEFT)

button5 = Button(middleframe, padx = 16, pady = 16, bd = 8, text = '5', command = lambda:set_text('5'))
button5.pack(side = LEFT)

button6 = Button(middleframe, padx = 16, pady = 16, bd = 8, text = '6', command = lambda:set_text('6'))
button6.pack(side = LEFT)

minus = Button(middleframe, padx = 16, pady = 16, bd = 8, text = '-', command = lambda:set_text('-'))
minus.pack(side = LEFT)

bottomframe = Frame(a)
bottomframe.pack(side = TOP)

button7 = Button(bottomframe, padx = 16, pady = 16, bd = 8, text = '7', command = lambda:set_text('7'))
button7.pack(side = LEFT)

button8 = Button(bottomframe, padx = 16, pady = 16, bd = 8, text = '8', command = lambda:set_text('8'))
button8.pack(side = LEFT)

button9 = Button(bottomframe, padx = 16, pady = 16, bd = 8, text = '9', command = lambda:set_text('9'))
button9.pack(side = LEFT)

times = Button(bottomframe, padx = 16, pady = 16, bd = 8, text = 'x', command = lambda:set_text('*'))
times.pack(side = LEFT)

morebottom = Frame(a)
morebottom.pack(side = TOP)

equals = Button(morebottom, padx = 16, pady = 16, bd = 8, text = '=', command = equals)
equals.pack(side = LEFT)

button0 = Button(morebottom, padx = 16, pady = 16, bd = 8, text = '0', command = lambda:set_text('0'))
button0.pack(side = LEFT)

clearbu = Button(morebottom, padx = 16, pady = 16, bd = 8, text = 'C', command = clear)
clearbu.pack(side = LEFT)

div = Button(morebottom, padx = 16, pady = 16, bd = 8, text = '/', command = lambda:set_text('/'))
div.pack(side = LEFT)

evenmore = Frame(a)
evenmore.pack(side = TOP)

cebut = Button(evenmore, padx = 16, pady = 16, bd = 8, text = 'CE', command = clear_all)
cebut.pack(side = LEFT)

decimal = Button(evenmore, padx = 16, pady = 16, bd = 8, text = '.', command = lambda:set_text('.'))
decimal.pack(side = LEFT)

power = Button(evenmore, padx = 16, pady = 16, bd = 8, text = '^', command = lambda:set_text('^'))
power.pack(side = LEFT)

a.mainloop

^ 是异或,使用 ** 代替 Python.

的幂

power = Button(evenmore, padx = 16, pady = 16, bd = 8, text = '^', command = lambda:set_text('^')) 中的 ** 替换最后的 ^ 会给你正确的结果,但它会在你的计算器显示屏上显示 **。因此,您可以将 result = eval(textDisplay.get()) 替换为 result = eval(textDisplay.get().replace('^', '**')),以便显示预期的符号,但答案是正确的。