1. 程式人生 > >python中Tkinter練習(計算器)

python中Tkinter練習(計算器)

今天看了小甲魚的視訊,發現了計算器還可以這樣做,感覺很奇葩,另外學習了下,

小甲魚原版程式碼如下:

#!/usr/bin/python
#coding: utf-8

from Tkinter import *

root = Tk()

frame = Frame(root)
frame.pack(padx = 10, pady = 10)

v1 = StringVar()
v2 = StringVar()
v3 = StringVar()

def test(content):
    # 如果是數字,返回真,否則返回假
    return content.isdigit()

# 把test函式包裝起來,用於validatecommand中

testCMD = root.register(test)
Entry(frame,
           textvariable = v1, # 設定當v1的值改變的時候,當前框體中的值也發生變化
           validate = "key",  # 當在輸入框輸入東西的時候直接進行驗證
           validatecommand = (testCMD, "%P"),
           ).grid(row = 0, column = 0)

Label(frame, text = "+").grid(row = 0, column = 1)

Entry(frame,
           textvariable = v2,
           validate = "key",
           validatecommand = (testCMD, "%P"),
           ).grid(row = 0, column = 2)

Label(frame, text = "=").grid(row = 0, column = 3)

Entry(frame,
           textvariable = v3,
           state = "readonly" # 設定不能修改這個框體中的值
           ).grid(row = 0, column = 4)

def calc():
    result = int(v1.get()) + int(v2.get())
    return v3.set(str(result))

Button(frame, text = "計算結果", command = calc).grid(row = 1, column = 2)

root.mainloop()


相對來說,個人認為,上面的程式碼加上一個退出,再加一個單選框,用來選擇哪一種運算子來進行操作,效果會更好,於是實踐了一把,程式碼如下:
#!/usr/bin/python
#coding: utf-8

from Tkinter import *

root = Tk()

frame = Frame(root)
frame.pack(padx = 10, pady = 10)

# Label(frame, text = "簡易計算器", font = ("宋體", 20)).grid(row = 0, column = 2)

v1 = StringVar()
v2 = StringVar()
v3 = StringVar()

def test(content):
    # 如果是數字,返回真,否則返回假
    return content.isdigit()

# 把test函式包裝起來,用於validatecommand中
testCMD = root.register(test)
Entry(frame,
           textvariable = v1, # 設定當v1的值改變的時候,當前框體中的值也發生變化
           validate = "key",  # 當在輸入框輸入東西的時候直接進行驗證
           validatecommand = (testCMD, "%P"),
           ).grid(row = 2, column = 0)

v = IntVar()
calcs = [
    ('+', 1),
    ('-', 2),
    ("*", 3),
    ("/", 4),
    ("%", 5)
]
for _calc, num in calcs:
    # 設定單選框,用來顯示運算子
    Radiobutton(frame,
                text = _calc,
                variable = v,
                value = num).grid(row = num - 1, column = 1, sticky = W)

Entry(frame,
           textvariable = v2,
           validate = "key",
           validatecommand = (testCMD, "%P"),
           ).grid(row = 2, column = 2)

Label(frame, text = "=").grid(row = 2, column = 3)

Entry(frame,
           textvariable = v3,
           state = "readonly" # 設定不能修改這個框體中的值
           ).grid(row = 2, column = 4)


def judge(x, y, z):
    if y == 1:
        return x + z
    elif y == 2:
        return x - z
    elif y == 3:
        return x * z
    elif y == 4:
        t = x / z
        return (x / z) if t * z == x else (x * 1.0 / z)
    elif y == 5:
        return x % z

def calc():
    # 根據calcs列表中的值來判斷此時的計算符號
    s = int(v.get())
    result = judge(int(v1.get()), s, int(v2.get()))
    return v3.set(str(result))

Button(frame, text = "計算結果",
       width = 10,
       command = calc).grid(row = 3, column = 2)
Button(frame,
       text = "退出",
       width = 10,
       command = root.quit).grid(row = 3, column = 4)

root.mainloop()


瞅一下執行介面(醜哭大笑)


當然,還可以增加更多的功能,把旁邊的那些沒用的區域找東西給替換了,這樣介面更完美,還有就是弄一個選單選項等等,有興趣的讀者自己去嘗試啦!!!!