【发布时间】:2017-09-15 20:53:31
【问题描述】:
所以我正在学习一点 Tkinter,现在我想做一个计算器。我知道我可以使用一些 for 循环更轻松地编写代码,但这是我的问题:当我按下等号按钮时,我的程序停止运行。当我离开“输入”时,它不会崩溃。你能帮我计算一下吗?这是代码(问题已标记):
import sys
from Tkinter import *
import math
BACK_COLOR = 'grey'
def add_one():
entry_box.insert(1000, str('1'))
def add_two():
entry_box.insert(1000, str('2'))
def add_three():
entry_box.insert(1000, str('3'))
def add_four():
entry_box.insert(1000, str('4'))
def add_five():
entry_box.insert(1000, str('5'))
def add_six():
entry_box.insert(1000, str('6'))
def add_seven():
entry_box.insert(1000, str('7'))
def add_eight():
entry_box.insert(1000, str('8'))
def add_nine():
entry_box.insert(1000, str('9'))
def add_zero():
entry_box.insert(1000, str('0'))
def add_point():
entry_box.insert(1000, str('.'))
def clean():
entry_box.delete(0, 'end')
def add_pi():
entry_box.insert(1000, str(math.pi))
def add_plus():
entry_box.insert(1000, str('+'))
def add_minus():
entry_box.insert(1000, str('-'))
def add_mult():
entry_box.insert(1000, str('*'))
def add_divide():
entry_box.insert(1000, str('/'))
def add_raise():
entry_box.insert(1000, str('^'))
def calculate():
#here is the problem
i = input(entry_box.get())
ab = int(i)
print(str(ab))
main_window = Tk()
main_window.resizable(False, False)
main_window.geometry('354x500')
main_window.title('Calculator Alpha')
main_window.configure(background=BACK_COLOR)
#logo
label_logo = Label(main_window, text='Calculator Alpha', padx=25,pady=10, background=BACK_COLOR)
label_logo.place(x=66, y=0)
label_logo.configure(font=('BlacklightD', 18))
#entrybox
entry_box = Entry(main_window, width=51)
entry_box.place(x=20, y=50)
#bottom row
button_equal = Button(main_window, text='=', padx=30, pady=25, command=calculate)
button_equal.place(x=277,y=430)
button_plus = Button(main_window, text='+', padx=30, pady=25, command=add_plus)
button_plus.place(x=207,y=430)
button_pi = Button(main_window, text='Pi', padx=30, pady=25, command=add_pi)
button_pi.place(x=137,y=430)
button_zero = Button(main_window, text='0', padx=30, pady=25, command=add_zero)
button_zero.place(x=67,y=430)
button_point = Button(main_window, text='.', padx=30, pady=25, command=add_point)
button_point.place(x=0,y=430)
#second bottom row
button_mult = Button(main_window, text='*', padx=30, pady=25, command=add_mult)
button_mult.place(x=280, y=356)
button_minus = Button(main_window, text='-', padx=30, pady=25, command=add_minus)
button_minus.place(x=210, y=356)
button_three = Button(main_window, text='3', padx=30, pady=25, command=add_three)
button_three.place(x=141, y=356)
button_two = Button(main_window, text='2', padx=29, pady=25, command = add_two)
button_two.place(x=69, y=356)
button_one = Button(main_window, text='1', padx=29, pady=25, command=add_one)
button_one.place(x=0, y=356)
#second top row
button_raise = Button(main_window, text='^', padx=30, pady=25, command=add_raise)
button_raise.place(x=277, y=282)
button_divide = Button(main_window, text='/', padx=30, pady=25, command=add_divide)
button_divide.place(x=210, y=282)
button_six = Button(main_window, text='6', padx=30, pady=25, command=add_six)
button_six.place(x=141, y=282)
button_five = Button(main_window, text='5', padx=30, pady=25, command=add_five)
button_five.place(x=67, y=282)
button_four = Button(main_window, text='4', padx=29, pady=25, command=add_four)
button_four.place(x=0, y=282)
#top row
button_delete = Button(main_window, text='<-', padx=27, pady=25)
button_delete.place(x=278, y=208)
button_clean = Button(main_window, text='C', padx=27, pady=25, command=clean)
button_clean.place(x=213, y=208)
button_nine = Button(main_window, text='9', padx=30, pady=25, command=add_nine)
button_nine.place(x=141, y=208)
button_eight = Button(main_window, text='8', padx=30, pady=25, command=add_eight)
button_eight.place(x=67, y=208)
button_seven = Button(main_window, text='7', padx=29, pady=25, command=add_seven)
button_seven.place(x=0, y=208)
main_window.mainloop()
【问题讨论】:
-
请将其缩减为minimal reproducible example。如果您单击等号按钮时程序崩溃,则出于此问题的目的,您可能不需要任何其他按钮。另外,你为什么打电话给
input?你认为这是在做什么? -
def calculate(): #这里有问题 i = input(entry_box.get()) ab = int(i) print(str(ab))
-
你得到的确切错误是什么?
-
为什么你会在 GUI 应用程序中使用
input?!那就是自找麻烦! -
当我单击等号按钮时程序冻结并等待我给我“pythonw.exe 没有反应”(这是翻译),这并不是真正的错误
标签: python tkinter crash calculator