本帖最後由 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) 插入屬性值 |