我正在使用tkinter在python中创建一个计算器。除了一件事,计算器工作得非常好。目前我已将计算器设置为清除显示。我想让它清除显示屏上的最后一个数字。例如564将变为56。
非常感谢任何帮助。
#Import tkinter
from Tkinter import *
#Setup quit button
def quit():
window.destroy()
#What happens when you click the button
def buttonclick(event):
global calcvalue
global savedvalue
global operator
pressed = ""
if event.x >10 and event.x <70 and event.y > 50 and event.y < 110 : pressed = 7
if event.x >10 and event.x <70 and event.y > 120 and event.y < 180 : pressed = 4
if event.x >10 and event.x <70 and event.y > 190 and event.y < 250 : pressed = 1
if event.x >10 and event.x <70 and event.y > 260 and event.y < 320 : pressed = 0
if event.x >80 and event.x <140 and event.y > 50 and event.y < 110 : pressed = 8
if event.x >80 and event.x <140 and event.y > 120 and event.y < 180 : pressed = 5
if event.x >80 and event.x <140 and event.y > 190 and event.y < 250 : pressed = 2
if event.x >150 and event.x <210 and event.y > 50 and event.y < 110 : pressed = 9
if event.x >150 and event.x <210 and event.y > 120 and event.y < 180 : pressed = 6
if event.x >150 and event.x <210 and event.y > 190 and event.y < 250 : pressed = 3
if event.x >80 and event.x <140 and event.y > 260 and event.y < 320 : pressed = "equals"
if event.x >150 and event.x <210 and event.y > 260 and event.y < 320 : pressed = "clear"
if event.x >220 and event.x <280 and event.y > 50 and event.y < 110 : pressed = "divide"
if event.x >220 and event.x <280 and event.y > 120 and event.y < 180 : pressed = "times"
if event.x >220 and event.x <280 and event.y > 190 and event.y < 250 : pressed = "minus"
if event.x >220 and event.x <280 and event.y > 260 and event.y < 320 : pressed = "plus"
if pressed == 0 or pressed == 1 or pressed == 2 or pressed == 3 or pressed == 4 or pressed == 5 or pressed == 6 or pressed == 7 or pressed == 8 or pressed == 9 :
calcvalue = calcvalue * 10 + pressed
if pressed == "divide" or pressed == "times" or pressed == "minus" or pressed == "plus" :
operator = pressed
savedvalue = calcvalue
calcvalue = 0
if pressed == "equals":
if operator == "divide": calcvalue = savedvalue /calcvalue
if operator == "times": calcvalue = savedvalue * calcvalue
if operator == "minus": calcvalue = savedvalue - calcvalue
if operator == "plus": calcvalue = savedvalue + calcvalue
if pressed == "clear":
calcvalue = 0
displayupdate()
canvas.update()
#Setup the display
def displayupdate():
canvas.create_rectangle(10, 10, 370, 40, fill="white", outline="black")
canvas.create_text(350, 25, text=calcvalue,font="Times 20 bold",anchor=E)
#Setup the canvas/window
def main():
global window
global tkinter
global canvas
window = Tk()
window.title("BIG Calculator")
Button(window, text="Quit", width=5, command=quit).pack()
canvas = Canvas(window, width= 380, height=330, bg = 'black')
canvas.bind("<Button-1>", buttonclick)
#Add the 1 2 3 4 5 6 7 8 9 0 buttons
canvas.create_rectangle(10, 50, 100, 110, fill="white", outline="black")
canvas.create_text(45, 80, text="7",font="Times 30 bold")
canvas.create_rectangle(10, 120, 100, 180, fill="white", outline="black")
canvas.create_text(45, 150, text="4",font="Times 30 bold")
canvas.create_rectangle(10, 190, 100, 250, fill="white", outline="black")
canvas.create_text(45, 220, text="1",font="Times 30 bold")
canvas.create_rectangle(10, 260, 100, 320, fill="white", outline="black")
canvas.create_text(45, 290, text="0",font="Times 30 bold")
canvas.create_rectangle(80, 50, 170, 110, fill="white", outline="black")
canvas.create_text(115, 80, text="8",font="Times 30 bold")
canvas.create_rectangle(80, 120, 170, 180, fill="white", outline="black")
canvas.create_text(115, 150, text="5",font="Times 30 bold")
canvas.create_rectangle(80, 190, 170, 250, fill="white", outline="black")
canvas.create_text(115, 220, text="2",font="Times 30 bold")
canvas.create_rectangle(150, 50, 240, 110, fill="white", outline="black")
canvas.create_text(185, 80, text="9",font="Times 30 bold")
canvas.create_rectangle(150, 120, 240, 180, fill="white", outline="black")
canvas.create_text(185, 150, text="6",font="Times 30 bold")
canvas.create_rectangle(150, 190, 240, 250, fill="white", outline="black")
canvas.create_text(185, 220, text="3",font="Times 30 bold")
#SHow the = c + - x / buttons
canvas.create_rectangle(80, 260, 170, 320, fill="white", outline="black")
canvas.create_text(115, 290, text="=",font="Times 30 bold")
canvas.create_rectangle(150, 260, 240, 320, fill="white", outline="black")
canvas.create_text(185, 290, text="C",font="Times 30 bold")
canvas.create_rectangle(220, 50, 370, 110, fill="white", outline="black")
canvas.create_text(295, 80, text="Divide",font="Times 30 bold")
canvas.create_rectangle(220, 120, 370, 180, fill="white", outline="black")
canvas.create_text(295, 150, text="Times",font="Times 30 bold")
canvas.create_rectangle(220, 190, 370, 250, fill="white", outline="black")
canvas.create_text(295, 220, text="Minus",font="Times 30 bold")
canvas.create_rectangle(220, 260, 370, 320, fill="white", outline="black")
canvas.create_text(295, 290, text="Plus",font="Times 30 bold")
#Make the whole calculator work
canvas.create_rectangle(10, 10, 280, 40, fill="white", outline="black")
global calcvalue
calcvalue = 0
displayupdate()
canvas.pack()
window.mainloop()
main()
答案 0 :(得分:1)
简单地说:你得到了ValueError,因为你试图在
中做int("clear")
if pressed == "clear": calcvalue = calcvalue - int(pressed) / 10
你可以这样做:
if pressed == "clear": calcvalue = int(calcvalue/10.0)
因为你只使用整数而你使用Python 2.x,你甚至可以这样做:
if pressed == "clear": calcvalue = calcvalue/10
在Python 2.x中:
integer/integer
始终提供integer
float/integer
和integer/float
提供float
在Python 3.x中:
integer/integer
始终提供float
integer//integer
始终提供integer
顺便说一下:
你可以这样:
if event.x >10 and event.x <70 ...
替换为:
if 10< event.x <70 ...
而且:
if pressed == 0 or pressed == 1 or pressed == 2 or pressed == 3 or pressed == 4 or pressed == 5 or pressed == 6 or pressed == 7 or pressed == 8 or pressed == 9 :
if pressed == "divide" or pressed == "times" or pressed == "minus" or pressed == "plus" :
用这个:
if pressed in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9):
if pressed in ("divide", "times", "minus", "plus"):
它更具可读性。