我正在编写我的第一个“大”项目——带有 Tkinter UI 的简单计算器。完成视觉后,我遇到了将数字/数学运算符发送到计算器屏幕的问题。当我尝试通过参数将数字发送到 adder() 时,它不会做任何事情(adder 是一个创建将发送到屏幕和数学求解器的字符串的函数)。def eq 没有完成,但即使没有它,屏幕也应该可以工作。请帮忙
顺便说一句,我不明白为什么 screen_out 似乎不在加法器中(这就是我使用全局的原因)
编码:
from tkinter import *
# logical part
screen_out = ''
def adder(ele):
global screen_out
screen_out += str(ele)
screen.configure(text=f"{screen_out}")
def deleter():
global screen_out
screen_out = screen_out[:-1]
screen.configure(text=f"{screen_out}")
def cleaner():
global screen_out
screen_out = ''
screen.configure(text=f"{screen_out}")
def eq():
global task
task = int(screen_out)
# main window
window = Tk()
window.title('Basic Calculator')
window.geometry('400x350')
# output screen
black_lbl = Label(window, width=1, height=2, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8')
black_lbl.place(x=390, y=0)
screen_former = Label(window, text="", width=6, height=2, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8')
screen_former.grid(column=0, row=0)
screen = Label(window, text="", width=27, height=2, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', anchor="e")
screen.place(x=-21, y=0)
# keyboard interface
btn9 = Button(window, text="9", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=adder('9'))
btn8 = Button(window, text="8", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('8'))
btn7 = Button(window, text="7", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('7'))
btn6 = Button(window, text="6", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('6'))
btn5 = Button(window, text="5", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('5'))
btn4 = Button(window, text="4", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('4'))
btn3 = Button(window, text="3", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('3'))
btn2 = Button(window, text="2", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('2'))
btn1 = Button(window, text="1", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('1'))
btn0 = Button(window, text="0", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('0'))
btn_dot = Button(window, text=".", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('.'))
btn_eq = Button(window, text="=", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=eq)
btn_plus = Button(window, text="+", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('+'))
btn_minus = Button(window, text="-", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('-'))
btn_mult = Button(window, text="*", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('*'))
btn_div = Button(window, text="/", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('/'))
btn_par_open = Button(window, text="(", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('('))
btn_par_clos = Button(window, text=")", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder(')'))
btn_clear = Button(window, text="C", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=cleaner)
btn_del = Button(window, text="DEL", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=deleter)
u = 0
if u == 0:
cleaner()
u += 1
btn_par_open.grid(column=0, row=1)
btn_par_clos.grid(column=1, row=1)
btn_clear.grid(column=2, row=1)
btn_del.grid(column=3, row=1)
btn7.grid(column=0, row=2)
btn8.grid(column=1, row=2)
btn9.grid(column=2, row=2)
btn_plus.grid(column=3, row=2)
btn4.grid(column=0, row=3)
btn5.grid(column=1, row=3)
btn6.grid(column=2, row=3)
btn_minus.grid(column=3, row=3)
btn1.grid(column=0, row=4)
btn2.grid(column=1, row=4)
btn3.grid(column=2, row=4)
btn_mult.grid(column=3, row=4)
btn_eq.grid(column=0, row=5)
btn0.grid(column=1, row=5)
btn_dot.grid(column=2, row=5)
btn_div.grid(column=3, row=5)
window.mainloop()