在python-tkinter-calcum中按1清除按钮backspace

2024-09-25 00:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用python语言用tkinter做一个计算器。除了一件事外,这个计算器工作得很好。目前我已将计算器设置为清除显示。我想让它清除显示屏上的最后一个号码。例如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()

Tags: andtexteventifcreatefillcanvasblack
1条回答
网友
1楼 · 发布于 2024-09-25 00:26:36

简而言之:您得到ValueError是因为您试图在中执行int("clear")

if pressed == "clear": calcvalue = calcvalue - int(pressed) / 10

您可以这样做:

^{pr2}$

因为您只处理整数,并且使用Python 2.x,因此您甚至可以执行以下操作:

if pressed == "clear": calcvalue = calcvalue/10

在Python 2.x中:

  • integer/integer总是integer
  • float/integer和{}给出{}

在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"):

它更具可读性。在

相关问题 更多 >