我想知道如果你可以幫助我在ttk中的樣式選項問題。我已經設法將大部分基本ttk小部件更改爲我偏好的樣式。我只是在改變滾動條的樣式。我搜索了幾個小時尋找答案,不幸的是無濟於事。更改tkinter中滾動條的外觀(使用ttk樣式)
下面是一個使用滾動條樣式選項示例代碼:
import tkinter as tk
from tkinter import ttk
class Gui:
def __init__(self,mainframe):
#set the style
style = ttk.Style()
style.configure('Horizontal.TScrollbar',background = "blue")
#Create a mainframe
self.mainframe = mainframe
self.mainframe.title("example")
#creating scrollbar frame
scrl_attr_frame = ttk.Frame(self.mainframe)
scrl_attr_frame.grid(column=0,row=5,sticky="ns")
scrl_attr_frame.rowconfigure(0, weight=1)
attr_canvas = tk.Canvas(scrl_attr_frame)
h_scroll = ttk.Scrollbar(scrl_attr_frame,orient="horizontal", command=attr_canvas.xview)
attr_canvas.configure(xscrollcommand=h_scroll.set)
attr_canvas.grid(column=0,row=0,sticky="ns")
h_scroll.grid(column=0, row=1,sticky="we")
attr_frame = ttk.Frame(attr_canvas)
attr_frame.grid(column=0,row=0,sticky="ns")
attr_canvas.create_window((0,0),window=attr_frame, anchor='nw')
attr_frame.bind("<Configure>",lambda event, canvas=attr_canvas : canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=200,takefocus=False,highlightthickness=0))#attribute_frame.winfo_height()/20,highlightthickness=0))
#setup treeview widget
tree_columns = ("c1", "c2", "c3")
self.tree = ttk.Treeview(attr_frame,columns=tree_columns, show="headings",takefocus=False)
self.tree.grid(column=0, row=0, sticky='nsew')
for head in tree_columns:
self.tree.heading(head,text=head,anchor="w")
root = tk.Tk()
myapp = Gui(root)
root.mainloop()
我也嘗試過多種組合,包括;
style.configure('TScrollbar',background='blue')
#and
style.configure('CustomScroll.Horizontal.TScrollbar',background='blue')
#in combination with
h_scroll = ttk.Scrollbar(scrl_attr_frame,orient="horizontal", command=attr_canvas.xview)
h_scroll['style'] = "CustomScroll.Horizontal.TScrollbar"
非常感謝您的幫助!
這將是,如果當有人創造的東西也給了與示例的完整文檔不錯,否則什麼都不做會更生產力。 – nbro 2015-02-06 22:33:43
什麼平臺?某些平臺上的某些原生小部件無法更改。 – 2015-02-06 22:44:39
@StephanL我想你是在Mac上,你甚至不能看到水平滾動條... – nbro 2015-02-06 22:54:48