Python圖形編程探索系列-07-程式
- 2020 年 1 月 19 日
- 筆記
設計任務
初步設計程式登錄介面,詳細分析設計步驟。
程式詳細分析
基本框架設計
import tkinter as tk import tkinter.messagebox root = tk.Tk() # 創建應用程式窗口 root.title("用戶登錄介面設計") root.geometry("230x100") # --------功能塊程式碼開始------- # --------功能塊程式碼結束------ root.mainloop()
設計標籤用於提示用戶
labelName = tk.Label(root, text='用戶姓名:', justify=tk.RIGHT, width=80) labelPwd = tk.Label(root, text='用戶密碼:', justify=tk.RIGHT, width=80)
設計輸入框
entryName = tk.Entry(root, width=80, textvariable=varName) entryPwd = tk.Entry(root, show='*', width=80, textvariable=varPwd)
設計按鈕
buttonOk = tk.Button(root, text='登錄', relief=tk.RAISED, command=login) buttonCancel = tk.Button(root, text='重置', relief=tk.RAISED, command=cancel) buttonquit = tk.Button(root, text='退出', relief=tk.RAISED, command=_quit)
設計功能函數
關聯變數
varName = tk.StringVar() varName.set('') varPwd = tk.StringVar() varPwd.set('')
登錄按鈕處理函數
def login(): # 獲取用戶名和密碼 name = entryName.get() pwd = entryPwd.get() if name == 'admin' and pwd == '123456': tk.messagebox.showinfo(title='Python tkinter', message='OK') else: tk.messagebox.showerror('Python tkinter', message='Error')
重新輸入按鈕處理函數
def cancel(): # 清空用戶輸入的用戶名和密碼 varName.set('') varPwd.set('')
退出按鈕處理函數
def _quit(): root.quit() root.destroy()
各個組件排兵布陣
labelName.place(x=10, y=5, width=80, height=20) labelPwd.place(x=10, y=30, width=80, height=20) entryName.place(x=100, y=5, width=80, height=20) entryPwd.place(x=100, y=30, width=80, height=20) buttonOk.place(x=30, y=70, width=50, height=20) buttonCancel.place(x=90, y=70, width=50, height=20) buttonquit.place(x=150, y=70, width=50, height=20)
完整程式組裝
import tkinter as tk import tkinter.messagebox root = tk.Tk() # 創建應用程式窗口 root.title("用戶登錄介面設計") root.geometry("230x100") # --------功能塊程式碼開始------- # 功能函數設計 varName = tk.StringVar() varName.set('') varPwd = tk.StringVar() varPwd.set('') def login(): # 獲取用戶名和密碼 name = entryName.get() pwd = entryPwd.get() if name == 'admin' and pwd == '123456': tk.messagebox.showinfo(title='Python tkinter', message='OK') else: tk.messagebox.showerror('Python tkinter', message='Error') def cancel(): # 清空用戶輸入的用戶名和密碼 varName.set('') varPwd.set('') def _quit(): root.quit() root.destroy() # 主窗口中的各個組件設計 labelName = tk.Label(root, text='用戶姓名:', justify=tk.RIGHT, width=80) labelPwd = tk.Label(root, text='用戶密碼:', justify=tk.RIGHT, width=80) entryName = tk.Entry(root, width=80, textvariable=varName) entryPwd = tk.Entry(root, show='*', width=80, textvariable=varPwd) buttonOk = tk.Button(root, text='登錄', relief=tk.RAISED, command=login) buttonCancel = tk.Button(root, text='重置', relief=tk.RAISED, command=cancel) buttonquit = tk.Button(root, text='退出', relief=tk.RAISED, command=_quit) # 主窗口中各個組件的排放位置 = 排兵布陣 labelName.place(x=10, y=5, width=80, height=20) labelPwd.place(x=10, y=30, width=80, height=20) entryName.place(x=100, y=5, width=80, height=20) entryPwd.place(x=100, y=30, width=80, height=20) buttonOk.place(x=30, y=70, width=50, height=20) buttonCancel.place(x=90, y=70, width=50, height=20) buttonquit.place(x=150, y=70, width=50, height=20) # --------功能塊程式碼結束------ root.mainloop() # 窗口運行循環