Tkinter教程之Entry篇

來源:互聯網
上載者:User
#Tkinter教程之Entry篇
#Entry用來輸入單行文本
'''1.第一個Entry程式'''
from Tkinter import *
root = Tk()
Entry(root,text = 'input your text here').pack()
root.mainloop()
#上面的代碼目的是建立一個Entry對象,並在Entry上顯示'input your text here',運行此代碼,並沒有看到文本的顯示,由此可知與Lable和Button不同,Entry的text屬性不可以設定Entry的文本

'''2.在Entry中設定初始值,使用textvariable將變數與Entry綁定'''
from Tkinter import *
root = Tk()
e = StringVar()
entry = Entry(root,textvariable = e)
e.set('input your text here')
entry.pack()
root.mainloop()

#上面的例子中將變數e與Entry綁定,然後將e的值設定為'input your text here',程式運行時的初始值便設定了。

'''3.設定為唯讀Entry.
Entry的另一個比較有用的屬性,設定為唯讀,不允許使用者對它的值改變。
設定state屬性為'readonly'
'''
from Tkinter import *
root = Tk()
e = StringVar()
entry = Entry(root,textvariable = e)
e.set('input your text here')
entry.pack()
entry['state'] = 'readonly'
root.mainloop()

#實際上Entry的屬性值可以使用的也為normal/active/disabled,'readonly'與disabled一樣

'''4.設定為密碼輸入框
#將Entry作為一個密碼輸入框來使用,即不顯示使用者輸入的內容值,用特定符號代替。使用用屬性
show來指定。
'''
from Tkinter import *
root = Tk()
e = StringVar()
entry = Entry(root,textvariable = e)
e.set('input your text here')
entry.pack()
#使用*來顯示輸入的內容,如果喜歡可以改為其它字元
entry['show'] = '*'
#分別使用*#$顯示輸入的常值內容
for mask in ['*','#','$']:
    e = StringVar()
    entry = Entry(root,textvariable = e)
    e.set('password')
    entry.pack()
    entry['show'] = mask

root.mainloop()

'''5.驗證輸入的內容是否符合要求。
使用validate來校正輸入的內容
使用validate方法來限制輸入的內容
這是一個有問題的例子,無法調用validateText回呼函數
‘'''
from Tkinter import *
root = Tk()
e = StringVar()
def validateText(contents):
    print contents
    return contents.isalnum()

entry = Entry(root,validate = 'key',textvariable = e,validatecommand = validateText)
entry.pack()

root.mainloop()
'''
文檔中說明使用validate來接受的事件,使用validatecommand來確定輸入的內容是否合法,但
如何傳入參數?沒找到相應的說明
'''
#還有其他的屬性fg/bg/relief/width/height/justify/state使用方法與Button相同,不再舉例。

#author:     jcodeer
#blog:    jcodeer.cublog.cn
#email:    jcodeer@126.com

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.