一、參數說明
語法 | 作用 |
---|---|
Entry(root,width=20) | 組件的寬度(所占字符個數) |
Entry(root,fg='blue') | 前景字體顏色 |
Entry(root,bg='blue') | 背景顏色 |
Entry(root,show="*") | 將Entry框中的文本替換為指定字符 |
Entry(root,state=readonly) | 設置組件狀態,默認為normal,可設置為:disabled—禁用組件,readonly—只讀 |
Entry(root,textvariable=text) | 指定變量,需要事先定義一個變量,在Entry進行綁定獲取變量的值 text=tk.StringVar() |
二、代碼示例
import tkinter as tk window = tk.Tk() def main(): global window # 設置主窗體大小 winWidth = 600 winHeight = 400 # 獲取屏幕分辨率 screenWidth = window.winfo_screenwidth() screenHeight = window.winfo_screenheight() # 計算主窗口在屏幕上的坐標 x = int((screenWidth - winWidth)/ 2) y = int((screenHeight - winHeight) / 2) # 設置主窗口標題 window.title("Entry輸入框參數說明") # 設置主窗口大小 window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y)) # 設置窗口寬高固定 window.resizable(0,0) # 設置窗口圖標 window.iconbitmap("./image/icon.ico") """entry參數. Valid resource names: background, bd, bg, borderwidth, cursor, exportselection, fg, font, foreground, highlightbackground, highlightcolor, highlightthickness, insertbackground, insertborderwidth, insertofftime, insertontime, insertwidth, invalidcommand, invcmd, justify, relief, selectbackground, selectborderwidth, selectforeground, show, state, takefocus, textvariable, validate, validatecommand, vcmd, width, xscrollcommand.""" var = tk.StringVar() var.set("請輸入密碼") # 當鼠標移入輸入框時,執行validatecommand tk.Entry(window, width=30, borderwidth=1, fg="#f00",insertwidth=1, insertbackground="#333", state=tk.NORMAL, textvariable=var, validate="focus", validatecommand=valid).pack() window.mainloop() def valid(): print("valid") if __name__ == '__main__': main()
三、效果圖