python tkinter-按鈕.標簽.文本框、輸入框


 

按鈕

無功能按鈕

Button的text屬性顯示按鈕上的文本
tkinter.Button(form, text='hello button').pack() 

無論怎么變幻窗體大小,永遠都在窗體的最上行的居中位置

 

點擊觸發事件

Button 的 command屬性調用方法,來執行事件

例如有個方法

def a():
    print ('已點擊按鈕')

 

tkinter.Button(form, text='hello button',command=a).pack() 

 點擊3次按鈕,執行了3次 a方法

 

設置按鈕的寬、高  width,height 屬性

方法一

tkinter.Button(form, text='hello button',width=10,height=1).pack()  

 

或者(注意第一行沒有.pack())

t1=tkinter.Button(form, text='button')

方法二

t1['width']=20
t1['height']=2
t1.pack()

方法三

t1.configure(width = 30,height = 3)
t1.pack()

 

按鈕狀態 state 屬性

默認是 NORMAL,還有一個狀態是active目前不知道什么作用

禁用

tkinter.Button(form, text='hello button',width=10,height=1,state=tkinter.DISABLED).pack()  

 

按鈕的前景色與背景色 

fg:  前景色(字體顏色)

tkinter.Button(form, text='hello button',width=10,height=1,fg='red').pack()  

 

 bg:背景色 

tkinter.Button(form, text='hello button',width=10,height=1,bg='blue').pack()

 

 文本在按鈕上的顯示位置

屬性 anchor

它的值有這8個方向

n(north),s(south),w(west),e(east)和ne,nw,se,sw,

 已西北方向為例子

tkinter.Button(form, text='hello button',width=20,height=5,anchor='nw').pack()

 

 按鈕風格

屬性 relief

tkinter.Button(form, text='hello button', relief=FLAT).pack()

測試沒成功。。。。。。????

 

 

標簽Label

lab1=tkinter.Label(form,text='標簽:').pack()

 

 

 

文本框 text

t1=tkinter.Text(form,width = 10,height = 1).grid(row=0,column=1)

 給文本框賦值

t1.insert(1.0,'abc')

那個1.0是什么意思,暫時不懂

 

取出文本框的值

 

 

輸入框 Entry

 

給輸入框賦值初始值

綁定tkinter.StringVar()后 set()

t1 = tkinter.StringVar()
t1.set('春季里那個百花開')
entry = tkinter.Entry(root, textvariable = t1).pack()
print (t1.get())

 

 

獲取輸入框的值 t1.get() 並打印


常用軟件開發學習資料目錄:  

1.經典編程電子書收藏  

2.C&C++編程學習資料收藏   

3.算法及數據結構(有關c,c++,java)   

4.Java開發學習資料收藏      

5.Android開發學習資料收藏  

6.Python開發學習資料收藏  

7.大數據,機器學習,人工智能資料收藏

8.Docker資料收藏


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM