登錄界面設計和功能實現
Entry單行文本框
Entry用來接收一行單行字符串控件, 如果用戶輸入的長度長於Entry控件的長度時, 文字會自動向后滾動, 如果想輸入多行文本, 則需要多行控件
1 # coding:utf-8 2 from tkinter import * 3 from tkinter import messagebox 4 5 6 class Application(Frame): 7 """一個經典的GUI程序類寫法""" 8 def __init__(self, master=None): 9 super().__init__(master) # super代表的是父類的定義,而不是父類的對象 10 self.master = master 11 self.pack() 12 self.createWidget() 13 14 def createWidget(self): 15 """創建登錄界面組件""" 16 self.label01 = Label(self, text='用戶名') 17 self.label01.pack() 18 # StringVar變量綁定到指定組件 19 # StringVar變量的值發生變化,組件內容也發生變化 20 # 組件內容發生變化, 變量的值也發生變化 21 v1 = StringVar() 22 self.entry01 = Entry(self, textvariable=v1) 23 self.entry01.pack() 24 v1.set('admin') 25 # 創建密碼框 26 self.label02 = Label(self, text='密碼') 27 self.label02.pack() 28 v2 = StringVar() 29 self.entry02 = Entry(self, textvariable=v2, show='*') 30 self.entry02.pack() 31 self.btn01 = Button(self, text='登錄', command=self.login) 32 self.btn01.pack() 33 34 35 36 def login(self): 37 username = self.entry01.get() 38 pwd = self.entry02.get() 39 if username == 'Liran' and pwd == '1314520': 40 messagebox.showinfo('命名系統', '登錄成功!') 41 else: 42 messagebox.showinfo('命名系統', '登錄失敗') 43 44 45 if __name__ == "__main__": 46 root = Tk() 47 root.geometry("400x450+200+300") 48 root.title('測試') 49 app = Application(master=root) 50 root.mainloop()