網(wǎng)站建設(shè)需不需要編程企業(yè)培訓(xùn)機構(gòu)
在本文中,將介紹 tkinter Frame 框架小部件、 LabelFrame 標簽框架小部件的使用方法。
Frame 框架
Frame 框架在窗體上建立一個矩形區(qū)域,作為一個容器,用于組織分組排列其他小部件。
要創(chuàng)建框架,請使用以下構(gòu)造函數(shù)。
frame = tk.Frame(master, **options)
tkinter 中的每個小部件都需要一個 “parent” 或 “master” 作為第一個參數(shù)。當使用框架時,要將框架作為其父級。
import?tkinter?as?tk
root?=?tk.Tk()
root.geometry('600x400+200+200')
root.title('Frame?框架演示')leftframe?=?tk.Frame(root)
leftframe.pack(side=tk.LEFT)rightframe?=?tk.Frame(root)
rightframe.pack(side=tk.RIGHT)button1?=?tk.Button(leftframe,?text?=?"Button1")
button1.pack(padx?=?10,?pady?=?10)
button2?=?tk.Button(rightframe,?text?=?"Button2")
button2.pack(padx?=?10,?pady?=?10)
button3?=?tk.Button(leftframe,?text?=?"Button3")
button3.pack(padx?=?10,?pady?=?10)root.mainloop()
在上面的代碼中,創(chuàng)建了leftframe、 rightframe 兩個框架并排左右放置,三個按鈕小部件分別放置到不同的框架中。
框架也可以作為分隔線使用。
import?tkinter?as?tk
root?=?tk.Tk()
root.geometry('600x400+200+200')
root.title('Frame?框架演示')
frame1?=?tk.Frame(root,?bd=5,?relief=tk.RIDGE)
frame1.pack(ipadx?=?100)button1?=?tk.Button(frame1,?text?=?"Button1")
button1.pack(padx?=?10,?pady?=?10)
button2?=?tk.Button(frame1,?text?=?"Button2")
button2.pack(padx?=?10,?pady?=?10)
button3?=?tk.Button(frame1,?text?=?"Button3")
button3.pack(padx?=?10,?pady?=?10)frame2?=?tk.Frame(frame1,?bd=5,?relief=tk.RIDGE,?bg="black")
frame2.pack(fill=tk.X)button4?=?tk.Button(frame1,?text?=?"Button1")
button4.pack(padx?=?10,?pady?=?10)
button5?=?tk.Button(frame1,?text?=?"Button2")
button5.pack(padx?=?10,?pady?=?10)
button6?=?tk.Button(frame1,?text?=?"Button3")
button6.pack(padx?=?10,?pady?=?10)
root.mainloop()
LabelFrame 標簽框架
tkinter 提供了 Frame 小部件的一個變體,稱為 LabelFrame。LabelFrame 標簽框架除了具備常規(guī)框架的功能外,還擴展了一些其他功能。
要創(chuàng)建框架,請使用以下構(gòu)造函數(shù)。
frame = tk.LabelFrame(master, **options)
LabelFrame 標簽框架增加了 Text 參數(shù),可以作為框架的標題。默認位置在左上角,也可以使用參數(shù) labelanchor ,改變標題的位置,可選參數(shù)如下圖所示。
使用 labelwidget 參數(shù),可以把其他小部件放到框架上。
import?tkinter?as?tk
root?=?tk.Tk()
root.geometry('600x400+200+200')
root.title('LabelFrame?標簽框架演示')button?=?tk.Button(root,?text?=?"Hello!")
button.pack(padx?=?5,?pady?=?6)LabelFrame1?=?tk.LabelFrame(root,?text='LabelFrame')
LabelFrame1.pack(ipadx?=?100)LabelFrame2?=?tk.LabelFrame(root,?text='LabelFrame',?labelanchor?=?"se")
LabelFrame2.pack(ipadx?=?100)LabelFrame3?=?tk.LabelFrame(root,?text='LabelFrame',?labelanchor?=?"s",?labelwidget?=?button)
LabelFrame3.pack(ipadx?=?100)button1?=?tk.Button(LabelFrame1,?text?=?"Button1")
button1.pack(padx?=?10,?pady?=?10)
button2?=?tk.Button(LabelFrame1,?text?=?"Button2")
button2.pack(padx?=?10,?pady?=?10)
button3?=?tk.Button(LabelFrame2,?text?=?"Button3")
button3.pack(padx?=?10,?pady?=?10)button4?=?tk.Button(LabelFrame2,?text?=?"Button4")
button4.pack(padx?=?10,?pady?=?10)
button5?=?tk.Button(LabelFrame3,?text?=?"Button5")
button5.pack(padx?=?10,?pady?=?10)
button6?=?tk.Button(LabelFrame3,?text?=?"Button6")
button6.pack(padx?=?10,?pady?=?10)
root.mainloop()
Frame LabelFrame 選項
選項名稱 | 說明 |
---|---|
bd | 邊框的寬度。默認 2 像素。 |
bg | 背景顏色。 |
cursor | 鼠標指針類型。 |
height | 框架的高度。 |
width | 框架的寬度。 |
highlightbackground | 背景顏色。 |
highlightthickness | 獲得焦點時邊框粗細。 |
relief | 框架的邊框類型。默認 FLAT。 |
highlightcolor | 獲得焦點時高亮顏色。 |