將捲軸附加至列表框而非 Tkinter 中的視窗
列表框小工具包含一個專案列表,例如數字或字元列表。假設希望使用列表框小工具建立長專案列表。然後,應該有一個合適的方法來檢視列表中的所有專案。在這種情況下,向列表框小工具新增捲軸將會很有幫助。
要新增新的捲軸,必須使用 Listbox(parent, bg, fg, width, height, bd, **options) 建構函式。建立列表框後,可以透過建立 Scrollbar(**options) 的物件,向其新增捲軸。
示例
#Import the required libraries
from tkinter import *
from tkinter import ttk
#Create an instance of Tkinter Frame
win = Tk()
#Set the geometry of Tkinter Frame
win.geometry("700x350")
#Create a vertical scrollbar
scrollbar= ttk.Scrollbar(win, orient= 'vertical')
scrollbar.pack(side= RIGHT, fill= BOTH)
#Add a Listbox Widget
listbox = Listbox(win, width= 350, bg= 'bisque')
listbox.pack(side= LEFT, fill= BOTH)
for values in range(100):
listbox.insert(END, values)
listbox.config(yscrollcommand= scrollbar.set)
#Configure the scrollbar
scrollbar.config(command= listbox.yview)
win.mainloop()輸出
執行上述程式碼將顯示一個包含列表框小工具的視窗,其中包含若干專案。垂直捲軸附加到了列表框小工具。

廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP