I combined a scrollbar with a ttk notebook by adapting this example and additionally porting it to Python3.
I am using ttk widgets as often as possible to get a more 'modern' UI. However, there is no ttk canvas widget, so I used the standard tkinter canvas. By default the backgroundcolor of the canvas widget seems to be white (at least on Mac OS), while the default background of all ttk widgets is grey (see screenshot below).
How can I get the default background of the ttk frame containing the label widgets, so that I can pass it to the canvas by Canvas(parent, background='bg_color')
and remove the white space around the ttk frame containing the ttk labels?
I know that ttk uses styles
to define the look of the widgets. However, I have no idea about how to read the background value of the (default) style.
#!/usr/bin/env python3
# coding: utf-8
from tkinter import *
from tkinter import ttk
master = Tk()
def populate():
"""Put in some fake data"""
for row in range(100):
ttk.Label(frame, text="%s" % row, width=3, borderwidth="1", relief="solid").grid(row=row, column=0)
t="this is the second column for row %s" % row
ttk.Label(frame, text=t).grid(row=row, column=1)
def OnFrameConfigure(event):
"""Reset the scroll region to encompass the inner frame"""
canvas.configure(scrollregion=canvas.bbox("all"))
nbook = ttk.Notebook(master)
nbook.grid()
tab = Frame(nbook)
canvas = Canvas(tab, borderwidth=0)
frame = ttk.Frame(canvas)
vsb = Scrollbar(tab, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
canvas.pack(side='left', fill='both', expand=True)
canvas.create_window((0, 0), window=frame, anchor='nw', tags='frame')
frame.bind("<Configure>", OnFrameConfigure)
populate()
nbook.add(tab, text='Test')
master.mainloop()
You can use s.lookup()
to get settings of a style. If you want the background setting of your ttk Frame use:
s = ttk.Style()
bg = s.lookup('TFrame', 'background')
Which you can apply to your Canvas:
canvas = Canvas(tab, borderwidth=0, background=bg)