標題:
圖形介面 (七) - 身分驗證
[打印本頁]
作者:
tonyh
時間:
2023-7-20 16:07
標題:
圖形介面 (七) - 身分驗證
本帖最後由 tonyh 於 2023-7-20 17:05 編輯
方法一:使用動態文字變數
import tkinter as tk
import tkinter.messagebox as tmbox
def check():
if name.get()=="Tony" and pw.get()=="456789":
tmbox.showinfo(title="驗證結果",message="您好,{}!".format(name.get()))
else:
tmbox.showerror(title="驗證結果",message="驗證失敗!")
def clear():
name.set("")
pw.set("")
win=tk.Tk()
win.title("身份驗證")
win.resizable(0,0)
name=tk.StringVar()
pw=tk.StringVar()
fm=tk.Frame(win)
lb=tk.Label(fm, text="來訪者:",width=7, anchor="e").pack(side="left")
et=tk.Entry(fm, textvariable=name).pack(side="left", padx=10, pady=10)
fm.pack()
fm=tk.Frame(win)
lb=tk.Label(fm, text="密碼:", width=7, anchor="e").pack(side="left")
et=tk.Entry(fm, textvariable=pw, show="*").pack(side="left", padx=10)
fm.pack()
fm=tk.Frame(win)
btn=tk.Button(fm, text="確定", width=12, bg="#CCEEFF", command=check).pack(side="left", padx=5, pady=20)
btn=tk.Button(fm, text="清除", width=12, bg="#CCEEFF", command=clear).pack(side="left", padx=5, pady=20)
fm.pack()
win.mainloop()
複製代碼
屬性 說明
-----------------------------------------------------
textvariable 設定動態文字變數
command 設定使用者觸發事件時要執行的函式
方法二:使用 get()、delete()、insert()
import tkinter as tk
import tkinter.messagebox as tmbox
def check():
if et1.get()=="Tony" and et2.get()=="456789":
tmbox.showinfo(title="驗證結果",message="您好,{}!".format(et1.get()))
else:
tmbox.showerror(title="驗證結果",message="驗證失敗!")
def clear():
et1.delete(0, tk.END)
et2.delete(0, tk.END)
#et1.insert(0, "hello")
win=tk.Tk()
win.title("身份驗證")
win.resizable(0,0)
fm=tk.Frame(win)
lb=tk.Label(fm, text="來訪者:",width=7, anchor="e").pack(side="left")
et1=tk.Entry(fm)
et1.pack(side="left", padx=10, pady=10)
fm.pack()
fm=tk.Frame(win)
lb=tk.Label(fm, text="密碼:", width=7, anchor="e").pack(side="left")
et2=tk.Entry(fm, show="*")
et2.pack(side="left", padx=10)
fm.pack()
fm=tk.Frame(win)
btn=tk.Button(fm, text="確定", width=12, bg="#CCEEFF", command=check).pack(side="left", padx=5, pady=20)
btn=tk.Button(fm, text="清除", width=12, bg="#CCEEFF", command=clear).pack(side="left", padx=5, pady=20)
fm.pack()
win.mainloop()
複製代碼
函式 說明
------------------------------------
get() 取得屬性值
delete(first,last) 刪除屬性值
insert(index,string) 插入屬性值
作者:
王捷恩
時間:
2023-7-20 16:53
import tkinter as tk
import tkinter.messagebox as tmbox
def check():
if name.get()=="yes" and pw.get()=="123":
tmbox.showinfo(title="驗證結果",message="您好,{}!".format(name.get()))
else:
tmbox.showerror(title="驗證結果",message="驗證失敗!")
def clear():
name.set("")
pw.set("")
win=tk.Tk()
win.title("身份驗證")
win.resizable(0,0)
name=tk.StringVar()
pw=tk.StringVar()
fm=tk.Frame(win)
lb=tk.Label(fm, text="來訪者:",width=7, anchor="e").pack(side="left")
et=tk.Entry(fm, textvariable=name).pack(side="left", padx=10, pady=10)
fm.pack()
fm=tk.Frame(win)
lb=tk.Label(fm, text="密碼:", width=7, anchor="e").pack(side="left")
et=tk.Entry(fm, textvariable=pw, show="*").pack(side="left", padx=10)
fm.pack()
fm=tk.Frame(win)
btn=tk.Button(fm, text="確定", width=12, bg="#CCEEFF", command=check).pack(side="left", padx=5, pady=20)
btn=tk.Button(fm, text="清除", width=12, bg="#CCEEFF", command=clear).pack(side="left", padx=5, pady=20)
fm.pack()
win.mainloop()
複製代碼
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="來訪者:",width=7, anchor="e").pack(side="left")
et1=tk.Entry(fm)
et1.pack(side="left", padx=10, pady=10)
fm.pack()
fm=tk.Frame(win)
lb=tk.Label(fm, text="密碼:", width=7, anchor="e").pack(side="left")
et2=tk.Entry(fm, show="*")
et2.pack(side="left", padx=10)
fm.pack()
def check():
if et1.get()=="yes" and et2.get()=="123":
tmbox.showinfo(title="驗證結果",message="您好,{}!".format(et1.get()))
else:
tmbox.showerror(title="驗證結果",message="驗證失敗!")
def clear():
et1.delete(0, tk.END)
et2.delete(0, tk.END)
fm=tk.Frame(win)
btn=tk.Button(fm, text="確定", width=12, bg="#CCEEFF", command=check).pack(side="left", padx=5, pady=20)
btn=tk.Button(fm, text="清除", width=12, bg="#CCEEFF", command=clear).pack(side="left", padx=5, pady=20)
fm.pack()
win.mainloop()
複製代碼
作者:
李沛昂
時間:
2023-7-20 16:55
import tkinter as tk
import tkinter.messagebox as tmbox
def check():
if name.get()=="Tony" and pw.get()=="1234567":
tmbox.showinfo(title="驗證結果",message="您好,{}!".format(name.get()))
else:
tmbox.showerror(title="驗證結果",message="驗證失敗!")
def clear():
name.set("")
pw.set("")
win=tk.Tk();
win.title("身分驗證")
win.resizable(0,0)
name=tk.StringVar()
pw=tk.StringVar()
fm=tk.Frame(win)
lb=tk.Label(fm,text="來訪者",width=7,anchor="e").pack(side="left")
dt=tk.Entry(fm,textvariable=name).pack(side="left",padx=10,pady=10)
fm.pack()
fm=tk.Frame(win)
lb=tk.Label(fm,text="密碼",width=7,anchor="e").pack(side="left")
dt=tk.Entry(fm,textvariable=pw,show="*").pack(side="left",padx=10,pady=10)
fm.pack()
fm=tk.Frame(win)
btn=tk.Button(fm,text="確定",width=12,bg="#CCEEFF",command=check).pack(side="left",padx=5,pady=20)
btn=tk.Button(fm,text="清除",width=12,bg="#CCEEFF",command=clear).pack(side="left",padx=5,pady=20)
fm.pack()
win.mainloop()
複製代碼
作者:
李沛昂
時間:
2023-7-20 17:10
import tkinter as tk
import tkinter.messagebox as tmbox
def check():
if et1.get()=="Tony" and et2.get()=="1234567":
tmbox.showinfo(title="驗證結果",message="您好,{}!".format(et1.get()))
else:
tmbox.showerror(title="驗證結果",message="驗證失敗!")
def clear():
et1.delete(0, tk.END)
et2.delete(0, tk.END)
win=tk.Tk();
win.title("身分驗證")
win.resizable(0,0)
name=tk.StringVar()
pw=tk.StringVar()
fm=tk.Frame(win)
lb=tk.Label(fm,text="來訪者",width=7,anchor="e").pack(side="left")
et1=tk.Entry(fm)
et1.pack(side="left",padx=10,pady=10)
fm.pack()
fm=tk.Frame(win)
lb=tk.Label(fm,text="密碼",width=7,anchor="e").pack(side="left")
et2=tk.Entry(fm,show="*")
et2.pack(side="left",padx=10)
fm.pack()
fm=tk.Frame(win)
btn=tk.Button(fm,text="確定",width=12,bg="#CCEEFF",command=check).pack(side="left",padx=5,pady=20)
btn=tk.Button(fm,text="清除",width=12,bg="#CCEEFF",command=clear).pack(side="left",padx=5,pady=20)
fm.pack()
win.mainloop()
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2