Tkinter 之ScrollBar滾動條標簽


一、參數說明

參數 作用
background (bg) 設置背景顏色
borderwidth (bd) 指定邊框寬度,通常是 2 像素
cursor  指定當鼠標在上方飄過的時候的鼠標樣式
orient 指定繪制 "horizontal"(垂直滾動條)還是 "vertical"(水平滾動條)
highlightbackground  指定當滾動條沒有獲得焦點的時候高亮邊框的顏色
highlightcolor   指定當滾動條獲得焦點的時候高亮邊框的顏色
jump  指定當用戶拖拽滾動條時的行為
relief   指定邊框樣式,  默認值是 "sunken"
command

 當滾動條更新時回調的函數
通常的是指定對應組件的 xview() 或 yview() 方法

takefocus  指定該組件是否接受輸入焦點(用戶可以通過 tab 鍵將焦點轉移上來),  默認值是 True
width 設置滾動條的寬度, 默認值是 16 像素

ScrollBar函數列表:

activate(element) 

-- 顯示 element 參數指定的元素的背景顏色和樣式

-- element 參數可以設置為:"arrow1"(箭頭1),"arrow2"(箭頭2)或 "slider"(滑塊)

delta(deltax, deltay)

-- 給定一個鼠標移動的范圍 deltax 和 deltay(像素為單位,deltax 表示水平移動量,deltay 表示垂直移動量),然后該方法返回一個浮點類型的值(范圍 -1.0 ~ 1.0)

-- 這通常在鼠標綁定上使用,用於確定當用戶拖拽鼠標時滑塊的如何移動

fraction(x, y)

-- 給定一個像素坐標 (x, y),該方法返回最接近給定坐標的滾動條位置(范圍 0.0 ~ 1.0)

get()

-- 返回當前滑塊的位置 (a, b)

-- a 值表示當前滑塊的頂端或左端的位置,b 值表示當前滑塊的底端或右端的位置(范圍 0.0 ~ 1.0)

identify(x, y)

-- 返回一個字符串表示指定位置下(如果有的話)的滾動條部件

-- 返回值可以是:"arrow1"(箭頭1),"arrow2"(箭頭2)、"slider"(滑塊)或 ""(啥都沒有)

set(*args)

-- 設置當前滾動條的位置

-- 如果設置則需要兩個參數 (first, last),first 表示當前滑塊的頂端或左端的位置,last 表示當前滑塊的底端或右端的位置(范圍 0.0 ~ 1.0)

二、代碼示例

import tkinter as tk

window = tk.Tk()
# 設置窗口大小
winWidth = 600
winHeight = 400
# 獲取屏幕分辨率
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()

x = int((screenWidth - winWidth) / 2)
y = int((screenHeight - winHeight) / 2)

# 設置主窗口標題
window.title("ScrollBar參數說明")
# 設置窗口初始位置在屏幕居中
window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
# 設置窗口圖標
window.iconbitmap("./image/icon.ico")
# 設置窗口寬高固定
window.resizable(0, 0)

"""scrollbar 參數.

        Valid resource names: activebackground, activerelief,
        background, bd, bg, borderwidth, command, cursor,
        elementborderwidth, highlightbackground,
        highlightcolor, highlightthickness, jump, orient,
        relief, repeatdelay, repeatinterval, takefocus,
        troughcolor, width."""
scroll_bar = tk.Scrollbar(window)
scroll_bar.pack(side = tk.RIGHT, fill = tk.Y)

list_box = tk.Listbox(window)

for i in range(1000):
    list_box.insert(tk.END, i)
list_box.config(yscrollcommand = scroll_bar.set)
list_box.pack(expand=1, fill = tk.BOTH)

scroll_bar.config(command = list_box.yview, width = 16)

window.mainloop()

 

三、效果圖

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM