- import tkinter as tk
- import tkinter.messagebox as tmbox
- win=tk.Tk()
- win.title("賣水果")
- win.resizable(0,0)
- lb=tk.Label(win, text="蓮霧", font=("微軟正黑體", 18), height=2).pack()
- fm=tk.Frame(win)
- et=tk.Entry(fm, font=("微軟正黑體", 16), width=10)
- et.pack(side="left")
- lb=tk.Label(fm, text=" 斤", font=("微軟正黑體", 14)).pack(side="left")
- fm.pack()
- def cal():
- str=et.get()
- try:
- qty=float(str)
- if qty>30:
- tmbox.showerror(title="輸入錯誤" , message="每人限購 30 斤!")
- et.delete(0, tk.END)
- return
- elif qty>=5:
- res=qty*40
- elif qty>=3:
- res=qty*45
- else:
- res=qty*50
- lb.config(text="%s 斤:%d 元"%(str, res))
- et.delete(0, tk.END)
- except:
- tmbox.showerror(title="輸入錯誤", message="請輸入數字!")
- et.delete(0, tk.END)
- btn=tk.Button(win, text=" 計 算 ", command=cal, font=("微軟正黑體", 12)).pack(pady=20)
- lb=tk.Label(win, bg="#FFFF77", fg="red", text="", font=("微軟正黑體", 16), width=20, height=2)
- lb.pack(padx=5,pady=5)
- win.mainloop()
-
複製代碼 |