iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 28
0
Software Development

從零開始的Python練成記系列 第 28

[Day 28]今天的我吃錯藥了:用GUI介面開發簡易計算機

  • 分享至 

  • xImage
  •  

因為已經沒梗的關係,就發生了這件事

因為前面都是以在開發工具端進行操作類型的程式,而就在自己真正接觸到GUI介面的開發設計時,就忽然有個想法告訴自己「是不是要自我挑戰一下」,所以就產出了這恐怖的東西高達142行的程式碼,連我都不得不佩服我自己的恆心讓我可以在短短的兩個小時內從無到有寫了出來 (更不用提還有排版的問題)

from tkinter import *
from tkinter import messagebox

main_win = Tk()
main_win.title("EZ Calculator")

# Definitions for functions below


class Returner:
    result = 0


def alerter_empty():
    messagebox.showwarning("Warning", "It can't be empty value for blanks!")

def alerter_format():
    messagebox.showwarning("Warning", "Wrong format, please check your input!")

#showwarning就是跳出警示型視窗的方法

def the_plus():
    if ent_front.get() is "" or ent_back.get() is "":
        alerter_empty()#我不希望在沒有值的時候有運算,所以就在這裡加了一個非空白的判斷
    else:
        fronter = ent_front.get()
        backer = ent_back.get()
        if fronter.isdigit() is False or backer.isdigit() is False:
            alerter_format()#同時,計算機最基本的就是不能有非數字的東西輸入
        else:
            Returner.result = float(fronter) + float(backer)


def the_minus():
    if ent_front.get() is "" or ent_back.get() is "":
        alerter_empty()
    else:
        fronter = ent_front.get()
        backer = ent_back.get()
        if fronter.isdigit() is False or backer.isdigit() is False:
            alerter_format()
        else:
            Returner.result = float(fronter) + float(backer)


def the_multi():
    if ent_front.get() is "" or ent_back.get() is "":
        alerter_empty()
    else:
        fronter = ent_front.get()
        backer = ent_back.get()
        if fronter.isdigit() is False or backer.isdigit() is False:
            alerter_format()
        else:
            Returner.result = float(fronter) + float(backer)


def the_divis():
    if ent_front.get() is "" or ent_back.get() is "":
        alerter_empty()
    else:
        fronter = ent_front.get()
        backer = ent_back.get()
        if fronter.isdigit() is False or backer.isdigit() is False:
            alerter_format()
        else:
            Returner.result = float(fronter) / float(backer)


def resulting():
    messagebox.showinfo("Result", "The result is " + str(Returner.result))
    #因為我的介面並沒有開發Result的輸出欄位,所以就只能透過彈出視窗來顯示了


def cleaning():
    ent_front.delete(first=0, last=len(ent_front.get()))
    ent_back.delete(first=0, last=len(ent_back.get()))
    Returner.result = 0
    #Clean的功能就直接把使用者輸入欄位與暗藏的Result欄位都清空

#End of function coding


top_frame = Frame(main_win)
top_frame.pack(side=TOP)

t_lft_frame = Frame(top_frame)
t_lft_frame.pack(side=LEFT)

t_rht_frame = Frame(top_frame)
t_rht_frame.pack(side=RIGHT)

btm_frame = Frame(main_win)
btm_frame.pack(side=BOTTOM)

b_top_frame = Frame(btm_frame)
b_top_frame.pack(side=TOP)
#透過Frame來讓程式版面變得比較好看一些

#Programs for top frame below

pane1 = PanedWindow(t_lft_frame, orient=VERTICAL)
pane1.pack(fill=X, expand=1)

pane2 = PanedWindow(t_lft_frame, orient=VERTICAL)
pane2.pack(fill=X, expand=1)

mainer = Label(pane1, text="Front number", width=15)
pane1.add(mainer)

sub_mainer = Label(pane2, text="Back number", width=15)
pane2.add(sub_mainer)

ent_front = Entry(t_rht_frame, bd=4)
ent_front.pack()

ent_back = Entry(t_rht_frame, bd=4)
ent_back.pack()

#End coding for top frame
#Programs for bottom frame below

btn_plus = Button(b_top_frame, text="  +  ", command=the_plus)
btn_plus.pack(side=LEFT)

btn_minus = Button(b_top_frame, text="  -  ", command=the_minus)
btn_minus.pack(side=LEFT)

btn_multi = Button(b_top_frame, text="  *  ", command=the_multi)
btn_multi.pack(side=LEFT)

btn_divis = Button(b_top_frame, text="  /  ", command=the_divis)
btn_divis.pack(side=LEFT)

btn_clean = Button(btm_frame, text="Clean", command=cleaning)
btn_clean.pack(side=LEFT)

btn_getter = Button(btm_frame, text="Result", command=resulting)
btn_getter.pack(side=LEFT)

btn_exit = Button(btm_frame, text="Close app", command=main_win.destroy)
btn_exit.pack(side=RIGHT)
#End coding for bottom frame

main_win.mainloop()

上圖時間:
https://ithelp.ithome.com.tw/upload/images/20191014/20120985DyVAvhxLwb.jpg
這是基本視窗的外觀

https://ithelp.ithome.com.tw/upload/images/20191014/20120985VQDGe9uLKA.jpg
同上面程式碼的註解,我有設計一個欄位不能為零的判斷,這時候就會跳出Warning的視窗

https://ithelp.ithome.com.tw/upload/images/20191014/20120985XE9OSJlDp8.jpg
一樣是Warning視窗,但這次是因為格式的問題

https://ithelp.ithome.com.tw/upload/images/20191014/20120985V19yTTXZ3f.jpg
在輸入正確格式的值,並按下+後再按下Result的結果

我真的是吃錯藥


上一篇
[Day 27]GUI程式設計再一波:彈出視窗與使用者輸入欄位
下一篇
[Day 29]昨天嗑藥殺腦細胞,今天就必須付出代價...
系列文
從零開始的Python練成記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言