記Python Tkinter控制元件學習1
阿新 • • 發佈:2019-01-14
Label——標籤控制元件,可顯示文字
- 引數
- win:父窗體
- text:顯示文字的內容
- bg:背景色
- fg:字型色
- font:font是一個元組
- wid:寬
- height:高
- wraplength:行寬
- justify:設定換行後的對齊方式
anchor:設定方位 n s w e center 可以組合使用
label = tkinter.Label(win, text='first Label', bg='blue', fg='red', font=('黑體', 25), wid=10, height=10, wraplength=100, justify='left', anchor='w' )
Button——按鈕控制元件
- 引數
- win:父窗體
- text:按鈕文字
command:點選按鈕執行動作(可以是lambda表示式或函式名)
def hello(): print('hello world') button = tkinter.Button(win, text="按鈕", command=hello)
Entry——輸入控制元件
- 引數
- show:密文顯示字元
e = tkinter.Variable()(e可理解為輸入框物件)
entry = tkinter.Entry(win, textvariable=e) e.set("value") #設定值 print(e.get()) #獲取值
Text——用於顯示多行文字
- 引數
- win:父窗體
- height:行數
- wid:行寬
text.insert()文字框內容插入方法
text = tkinter.Text(win, wid=30, height=4) str = 'I am the bone of my sword.Steel is my body,and fire is my blood.I have created over a thousand blades.Unknown to Death.Nor known to Life' text.insert(tkinter.INSERT, str)
滾動條
滾動條設定的關鍵在於滾動條和控制元件的關聯
import tkinter win = tkinter.Tk() text = tkinter.Text(win, wid=30, height=4) str = 'I am the bone of my sword.Steel is my body,and fire is my blood.I have created over a thousand blades.Unknown to Death.Nor known to Life' text.insert(tkinter.INSERT, str) scroll = tkinter.Scrollbar() # 設定位置 scroll.pack(side=tkinter.RIGHT, fill=tkinter.Y) text.pack(side=tkinter.LEFT, fill=tkinter.Y) # 關聯(此處的關聯的單方的) scroll.config(command=text.yview) #滾動條向文字框關聯 text.config(yscrollcommand=scroll.set) 文字框向滾動條關聯 win.mainloop()