ttk styles

P

Peter

Hi I'm struggling to get a good understanding of styles as used in
ttk. I have read the tutorial section on using styles but haven't been
able to solve this problem.

I am attempting to create a Checkbutton with the indicatoron=false
option. Using ttk the documentation is clear that you have to create a
custom style to achieve this behaviour. But the only "example" I have
been able to find on the Internet searches is written in Tcl i.e. here
is what I have found (quoted directly):

Here’s how you set it up: To achieve the effect of -indicatoron false,
create a new layout that doesn’t have an indicator:

style layout Toolbar.TCheckbutton {
Toolbutton.border -children {
Toolbutton.padding -children {
Toolbutton.label
}
}
}

Then use style map and style default to control the border appearance:

style default Toolbar.TCheckbutton \
-relief flat
style map Toolbar.TCheckbutton -relief {
disabled flat
selected sunken
pressed sunken
active raised

Hopefully somebody else in this group has "done" this and can post
their "solution"?

Thanks
Peter
 
P

Peter

Thanks for the link Malcolm, I'll have a look at it. What is
particularly interesting (at first glance), is that the author has
"mixed" Tkinter with ttk as it suited i.e. look at this line:

f1 = tkinter.Frame(nb, background="red")

If ttk was being used purely (from tkinter import *; from ttk import
*) then the "background" keyword is nolonger available/recognised and
the code would have to use ttk styles to change the colour - I find it
somewhat disappointing that the author felt this approach was
necessary as it tends to dilute the example by not keeping everything
purely ttk - modifying the style to change the background of the Frame
statements would have made it an even better example!

I will repost the answer if I can work it out using this example code
- thanks again! :)

Peter
 
E

Ethan Furman

Peter said:
Thanks for the link Malcolm, I'll have a look at it. What is
particularly interesting (at first glance), is that the author has
"mixed" Tkinter with ttk as it suited i.e. look at this line:

f1 = tkinter.Frame(nb, background="red")

If ttk was being used purely (from tkinter import *; from ttk import
*) then the "background" keyword is nolonger available/recognised and
the code would have to use ttk styles to change the colour - I find it
somewhat disappointing that the author felt this approach was
necessary as it tends to dilute the example by not keeping everything
purely ttk - modifying the style to change the background of the Frame
statements would have made it an even better example!

I will repost the answer if I can work it out using this example code
- thanks again! :)

Another place to look for inspiration is http://tkdocs.com/

One thing to keep in mind is that not all widgets in tkinter have been
migrated to ttk (although Frame was).

~Ethan~
 
P

Peter

Here is what I came up with - hopefully I have understood the process
correctly and therefore that the comments are correct :)

I am not sure I have the color of the indicator when it is (de)pressed
correct, but to my eyes the color 'snow' looks like the same color
used with a Tkinter Checkbutton with indicatoron=false.

r = Tk()

s = Style()

# create a layout that excludes the indicator element
s.layout('NoIndicator.TCheckbutton',
[('Checkbutton.border',
{'children': [('Checkbutton.padding',
{'children': [('Checkbutton.label',
{})]})]})])

# Now create(?) a 'setting' for the border appearance of the
checkbutton
s.theme_settings('default', {
'NoIndicator.TCheckbutton': {'configure': {'relief': ''}}})

# set the attributes of the 'setting' to provide the required
behaviour
s.map('NoIndicator.TCheckbutton',
relief=[('disabled', 'flat'),
('selected', 'sunken'),
('pressed', 'sunken'),
('active', 'raised'),
('!active', 'raised')],
background=[('selected', 'snow')])

button = Checkbutton(r,
text='Test',
style='NoIndicator.TCheckbutton')
button.pack()

r.mainloop()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top