劉任昌GUI計算機eval函數比較美國與印度程式碼

美國計算機

印度計算機

from tkinter import *
def button_press(num):
    global equation_text
    equation_text = equation_text + str(num)
    equation_label.set(equation_text)
def equals():
    global equation_text
    try:
        total = str(eval(equation_text))
        equation_label.set(total)
        equation_text = total
    except SyntaxError:
        equation_label.set("syntax error")
        equation_text = ""
    except ZeroDivisionError:
        equation_label.set("arithmetic error")
        equation_text = ""
def clear():
    global equation_text
    equation_label.set("")
    equation_text = ''
window = Tk()
window.title('劉任昌拷貝美國人的程式碼')
window.geometry("500x500")
equation_text = ""
equation_label = StringVar()
label = Label(window, textvariable=equation_label, font=('consolas',20), bg="white", width=24, height=2)
label.pack()
frame = Frame(window)
frame.pack()
button1 = Button(frame, text=1, height=4, width=9, font=35, command=lambda: button_press(1))
button1.grid(row=0, column=0)
button2 = Button(frame, text=2, height=4, width=9, font=35, command=lambda: button_press(2))
button2.grid(row=0, column=1)
button3 = Button(frame, text=3, height=4, width=9, font=35, command=lambda: button_press(3))
button3.grid(row=0, column=2)
button4 = Button(frame, text=4, height=4, width=9, font=35, command=lambda: button_press(4))
button4.grid(row=1, column=0)
button5 = Button(frame, text=5, height=4, width=9, font=35, command=lambda: button_press(5))
button5.grid(row=1, column=1)
button6 = Button(frame, text=6, height=4, width=9, font=35, command=lambda: button_press(6))
button6.grid(row=1, column=2)
button7 = Button(frame, text=7, height=4, width=9, font=35, command=lambda: button_press(7))
button7.grid(row=2, column=0)
button8 = Button(frame, text=8, height=4, width=9, font=35, command=lambda: button_press(8))
button8.grid(row=2, column=1)
button9 = Button(frame, text=9, height=4, width=9, font=35, command=lambda: button_press(9))
button9.grid(row=2, column=2)
button0 = Button(frame, text=0, height=4, width=9, font=35, command=lambda: button_press(0))
button0.grid(row=3, column=0)
plus = Button(frame, text='+', height=4, width=9, font=35, command=lambda: button_press('+'))
plus.grid(row=0, column=3)
minus = Button(frame, text='-減', height=4, width=9, font=35, command=lambda: button_press('-'))
minus.grid(row=1, column=3)
multiply = Button(frame, text='*乘', height=4, width=9, font=35, command=lambda: button_press('*'))
multiply.grid(row=2, column=3)
divide = Button(frame, text='/', height=4, width=9, font=35, command=lambda: button_press('/'))
divide.grid(row=3, column=3)
equal = Button(frame, text='=執行', height=4, width=9, font=40, command=equals)
equal.grid(row=3, column=2) #放在第三列第二攔
decimal = Button(frame, text='.', height=4, width=9, font=35, command=lambda: button_press('.'))
decimal.grid(row=3, column=1)
clear = Button(window, text='清除', height=4, width=12, font=35, command=clear)
clear.pack()
window.mainloop()

留言

  1. https://wilson11122.blogspot.com/2024/05/guieval.html

    回覆刪除
  2. https://whitexd.blogspot.com/2024/05/guieval.html

    回覆刪除
  3. https://kk0725.blogspot.com/2024/05/guieval.html

    回覆刪除
  4. https://ziyun040818.blogspot.com/2024/05/guieval.html

    回覆刪除
  5. https://yun0205.blogspot.com/2024/05/guieval.html

    回覆刪除
  6. https://asd0908449304.blogspot.com/2024/05/guieval.html

    回覆刪除
  7. https://ctz642895.blogspot.com/2024/05/guieval.html

    回覆刪除
  8. https://htmlcssjavascriptjavapython.blogspot.com/2024/05/guieval.html

    回覆刪除
  9. https://rainhtmlcssjavascriptjavapython.blogspot.com/2024/05/guieval.html

    回覆刪除
  10. https://5lujo.blogspot.com/2024/05/guieval-5-20-2024.html

    回覆刪除
  11. https://hui026.blogspot.com/2024/05/guieval.html

    回覆刪除
  12. https://aks8704100.blogspot.com/2024/05/guieval.html

    回覆刪除
  13. https://xi715.blogspot.com/2024/05/guieval.html

    回覆刪除
  14. https://keiime.blogspot.com/2024/05/guieval.html

    回覆刪除
  15. https://rrrrr-eeeee.blogspot.com/2024/05/from-tkinter-import-def-buttonpressnum.html

    回覆刪除
  16. https://oscarliu1688.blogspot.com/2024/05/guieval.html

    回覆刪除
  17. https://yuechen613.blogspot.com/2024/05/guieval.html

    回覆刪除
  18. https://amyxl6316.blogspot.com/2024/05/guieval.html

    回覆刪除
  19. https://njcjww.blogspot.com/2024/05/guieval.html

    回覆刪除
  20. https://the-dumbest-one.blogspot.com/2024/05/eval.html

    回覆刪除
  21. https://yyyyyyjuuuuuuuu.blogspot.com/2024/05/eval.html

    回覆刪除
  22. https://addison030.blogspot.com/2024/05/guieval.html

    回覆刪除

張貼留言

這個網誌中的熱門文章

柯聞折Javascript物件導向方法是物件的函數

甲劉任昌python串列List(陣列Array),元組Tuple,集合Set,字典Dict

劉任昌PYTHON陣列ARRAY串列LIST字典DIC集合SET元組TUPLE