本帖最後由 李沛昂 於 2023-8-12 17:05 編輯
- import tkinter as tk
- import tkinter.messagebox as tmbox
- win=tk.Tk()
- win.title("賣水果")
- win.resizable(0,0)
- fm=tk.Frame(win)
- lb=tk.Label(fm,text="蓮霧",font=("微軟正黑體",16)).pack(side="left",padx=100,pady=20)
- fm.pack()
- n=tk.IntVar()
- fm=tk.Frame(win)
- et=tk.Entry(fm,font=("微軟正黑體",16),width=10)
- et.pack(side="left",pady=5)
- lb=tk.Label(fm,text=" 斤",font=("微軟正黑體",16)).pack(side="left")
- fm.pack()
- def count():
- st=et.get()
- try:
- qty=float(st)
- 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
- except:
- tmbox.showerror(title="輸入錯誤",message="請輸入數字!")
- et.delete(0,tk.END)
- lb.config(text="%s 斤:%d 元"%(st, res))
- et.delete(0, tk.END)
- fm=tk.Frame(win)
- btn=tk.Button(fm,text=" 計 算 ",command=count,width=7,height=1,font=("微軟正黑體",14)).pack(side="left",pady=30)
- fm.pack()
- fm=tk.Frame(win)
- lb=tk.Label(fm,bg="yellow",fg="red",font=("微軟正黑體",16),width=20,height=2)
- lb.pack(padx=5,pady=5)
- fm.pack()
- win.mainloop()
複製代碼 |