2013-11-29 3 views
0

Я делаю калькулятор в python, используя tkinter. Калькулятор отлично работает, кроме одного. На данный момент я установил калькулятор для очистки дисплея. Я хочу сделать так, чтобы он очищал последний номер на дисплее. Например, 564 станет 56.Сделать прозрачную кнопку backspace одним из python tkinter calculator

Любая помощь очень ценится.

#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

Я изменил прозрачность на «код», если нажата == «clear»: calcvalue = calcvalue - int (нажата)/10 'code', но имеет строку 34, в buttonclick calcvalue = calcvalue - int (нажата)/10 ValueError: недействительный литерал для int() с базой 10: 'clear' ошибка. Пожалуйста, помогите как можно скорее – user1851922

ответ

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

Это более читаемым.

+0

GREAT! Большое спасибо за Вашу помощь! Теперь он отлично работает! – user1851922

+0

Вы всегда можете отметить мой ответ, как принято;) – furas

+0

Извините ... Там вы идете! :) – user1851922

Смежные вопросы