1. 程式人生 > >python3中給listbox新增垂直滾動條和水平滾動條

python3中給listbox新增垂直滾動條和水平滾動條

#scrolly是垂直滾動條,scrolly2是水平滾動條
scrolly=Scrollbar(win,width=25,orient=VERTICAL)
scrolly.grid(row=0,column=1,padx=(0,0),pady=(250,0),sticky=NS) #grid是一種佈局設定
#padx決定在該元件在本單元格中左右邊距,
#例如padx=20,則表示左右邊距都是20;padx=(20,10)則表示左邊距是20,右邊距是10。pady表示上下邊距.
#rowspan表示合併行,colums表示合併列。sticky表示對齊方式。
scrolly2=Scrollbar(win,width=25,orient=HORIZONTAL)
scrolly2.grid(row=1,column=0,padx=(30,0),pady=0,sticky=EW)

statesList = [“Connecticut”,“Maine”,“Masssachusetts”,“New Hampshired”,“Rhode ISsland”, “Vermont”,“Masssachusetts”,“New Hampshired”,“Rhode ISsland”, “Vermont”,“Masssachusetts”,“New Hampshired”,“Rhode ISsland”, “Vermont”,“Masssachusetts”,“New Hampshired”,“Rhode ISsland”, “Vermont”]
conOFlstNE = StringVar()
List1=Listbox(win,width=105,height=16,listvariable=conOFlstNE)
List1.grid(row=0,column=0,padx=(30,0),pady=(250,0),sticky=E)
List1[‘yscrollcommand’]=scrolly.set #注意垂直滾動條用yscrollcommand
List1[‘xscrollcommand’]=scrolly2.set #注意垂直滾動條用xscrollcommand

conOFlstNE.set(tuple(statesList))
scrolly[“command”] = List1.yview #注意垂直滾動條用yview
scrolly2[“command”] = List1.xview #注意垂直滾動條用yview