按鈕
無功能按鈕
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() 並打印