有人知道如何构建一个功能如下的动态计算器:
带按钮的GUI和4个显示文本小部件(Number1,Operator,Number2,Result)
必须推送下一个number2(如果多次推送,则追加number2)
如果" ="被推,打印结果
如果" ="没有按下,但是任何其他操作符(+ - ,/,)打印结果并保存下一个操作符, - 将结果保存为number1以进行下一步操作*
我是编码的初学者,经过几个月的学习,我决定使用tkinter在Python中创建一个计算器。我不得不说到现在为止我没有向任何人求助。
它可能不会起作用,但在下面你可以找到我尝试创建我的第一个程序..:
from tkinter import *
import operator
root = Tk()
display1 = None
display2 = None
number1 = None
number2 = None
result = None
op_char = None
op_func = None
op_display = None
T_num1 = Text(root, height=2, width=30)
T_num1.pack()
T_num2 = Text(root, height=2, width=30)
T_num2.pack()
T_op = Text(root, height=2, width=30)
T_op.pack()
T_result = Text(root,height=2, width=30)
T_result.pack()
ops = {"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.truediv}
def numberpush(input):
global result
global number1
global number2
global result
global op_char
global op_func
global op_display
global ops
global display1
global display2
if result != None:
if number1 != None:
if number2 == None:
number2 = input
display2 = input
result = op_func(display1, number2)
number1 = result
else:
number2.append(input)
display2.append(input)
T_num2.insert(END, display2)
result = op_func(display1, display2)
number1 = result
else:
number1 = result
display2 = result
T_num2.insert(END, display2)
elif operator_func == None:
if number1 == None:
number1 = input
display1 = input
T_num1.insert(END, display1)
else:
number1 = number1.append(input)
display1.append(input)
T_num1.insert(END, display1)
else:
number2 = input
display2 = input
T_num2.insert(END, display2)
result = op_func(number1, number2)
number1 = result # Set Number1 to Result
operator_func = None # Reset Operator to none
op_display = None
T_op.insert(END, op_display)
def buttonpush(input):
global result
global number1
global number2
global result
global op_char
global op_func
global op_display
global ops
global display1
global display2
if number1 == None:
pass
elif result != None:
# Check if Previous Calculation has been done, if so -> display the result of previous calc and save new operator
display1 = result
T_num1.insert(END, display1)
number2 = None
display2 = None
T_num2.insert(END, display2)
op_char = input
op_func = ops[op_char]
op_display = input
T_op.insert(END, op_display)
result = None # Reset the Result
else:
op_char = input
op_func = ops[op_char]
op_display = input
T_op.insert(END, op_display)
def result():
global result
global number1
global number2
global result
global op_char
global op_func
global op_display
global ops
global display1
global display2
if number1 !=None and number2 != None and op_func == None:
if result == None:
T_result.insert(END, number1)
else:
T_result.insert(END, result)
elif number1 != None and op_func != None:
T_result.insert(END, "Error")
elif number1 != None:
T_result.insert(END, number1)
else:
T_result.insert(END, "Waiting")
button_0 = Button(root, text="0", command=numberpush(0))
button_0.pack()
button_1 = Button(root, text="1", command=numberpush(1))
button_1.pack()
button_2 = Button(root, text="2", command=numberpush(2))
button_2.pack()
button_3 = Button(root, text="3", command=numberpush(3))
button_3.pack()
button_4 = Button(root, text="4", command=numberpush(4))
button_4.pack()
button_5 = Button(root, text="5", command=numberpush(5))
button_5.pack()
button_6 = Button(root, text="6", command=numberpush(6))
button_6.pack()
button_7 = Button(root, text="7", command=numberpush(7))
button_7.pack()
button_8 = Button(root, text="8", command=numberpush(8))
button_8.pack()
button_9 = Button(root, text="9", command=numberpush(9))
button_9.pack()
button_add = Button(root, text="+", command=buttonpush("+"))
button_add.pack()
button_subtract = Button(root, text="-", command=buttonpush("-"))
button_subtract.pack()
button_multiply = Button(root, text="*", command=buttonpush("*"))
button_multiply.pack()
button_divide = Button(root, text="/", command=buttonpush("/"))
button_divide.pack()
button_result = Button(root, text="=", command=result())
button_result.pack()
root.mainloop()