2 '''Entry編輯框 收集數據'''
3 import tkinter as tk
4 import tkinter.messagebox as messagebox
5 #初始化窗口
6 window = tk.Tk()
7 #窗口名稱
8 window.title("My Window")
9 #窗口大小,是 x 不是 *
10 window.geometry("400x400")
11 #不能改變窗口的大小
12 window.resizable(width=False,height=False)
13 #相當於動畫顯示
14 var = tk.StringVar()
15 #傳值
16 var.set("Hello World")
17 #創建title標簽,以便更清楚觀察
18 title = tk.Label(window,textvariable=var,height=2,width=30,bg="green",font=("宋體",15))
19 title.pack()
20 #創建容器
21 f_1 = tk.Frame(window)
22 f_1.place(x=100,y=50)
23 #標簽1
24 l_1 = tk.Label(f_1,text="number")
25 l_1.pack()
26 #標簽2
27 l_2 = tk.Label(f_1,text="password")
28 l_2.pack()
29 #創建容器
30 f_2 = tk.Frame(window)
31 f_2.place(x=170,y=50)
32 #編輯框1
33 e_1 = tk.Entry(f_2,width=20)
34 e_1.pack()
35 #編輯框2,隱藏輸入的內容
36 e_2 = tk.Entry(f_2,width=20,show="*")
37 e_2.pack()
38 #圖片標簽
39 label_image = tk.Label(f_2)
40 def log():
41 #判斷編輯框的內容
42 if e_1.get() == "123" and e_2.get() == "123":
43 image = tk.PhotoImage(file="3.gif")
44 #添加圖片
45 label_image.config(image=image)
46 label_image.pack()
47 #傳值
48 var.set("登陸成功")
49 #使用消息框,彈出對應的信息
50 messagebox.showinfo(title="提示",message="歡迎使用***")
51 else:
52 #傳值
53 var.set("賬號或密碼輸入錯誤!")
54 # 使用消息框,彈出對應的信息
55 messagebox.showerror(title="錯誤",message="你的輸入有誤!")
56 #創建登陸按鈕
57 button = tk.Button(f_2,text="confirm",command=log)
58 button.pack()
59 #循環窗口
60 window.mainloop()
運行
輸入錯誤密碼
輸入正確密碼