Warren Smith
Warren Smith

Reputation: 1

issues with themed styles with tkinter ttk

I have been working with styles and trying to understand the interface and I see three issues that I have identified with the following. I would appreciate it if someone could illuminate these issues for me.

First here is the information about th styles

                 
>>> style.theme_use('classic')
>>> style.theme_use()
'classic'
>>> style.configure('TButton')
{'relief': 'raised', 'padding': '3m 1m', 'anchor': 'center', 'shiftrelief': 1}

Issue 1 : Changing Button Text Using Styles

Look at the session below


#show the layout of the TButton style
>>> style.layout('TButton')
[('Button.highlight', {'sticky': 'nswe', 'children': [('Button.border', {'sticky': 'nswe', 'border': '1', 'children': [('Button.padding', {'sticky': 'nswe', 'children': [('Button.label', {'sticky': 'nswe'})]})]})]})]

# now show element options for all the elements in the TButton style
>>> style.element_options('Button.highlight')
('highlightcolor', 'highlightthickness')

>>> style.element_options('Button.border')
('background', 'borderwidth', 'relief', 'default')

>>> style.element_options('Button.padding')
('padding', 'relief', 'shiftrelief')

>>> style.element_options('Button.label')
('compound', 'space', 'text', 'font', 'foreground', 'underline', 'width', 'anchor', 'justify', 'wraplength', 'embossed', 'image', 'stipple', 'background')


#Now the element Button.label shows that it has an attribute of text and if you set that attribute with:

>>> style.configure('TButton', text = 'my button')
>>> style.configure('TButton')
{'relief': 'raised', 'padding': '3m 1m', 'anchor': 'center', 'text': 'my button', 'shiftrelief': 1}

I expected to be able to change a button’s text through style.configure('TButton', text='new button') , but this doesn't work. The text attribute appears in the style configuration, yet the button does not display the updated text.

Additionally, in older Python versions (e.g., 2.7), I could change a button’s text at runtime using:

button.configure(text='new text')

But in Python 3.10.4, when I run:

button = ttk.Button(root, style='TButton')
button.pack()
button.config(text='new button')

I get an error:


_tkinter.TclError: invalid command name ".!button"

So, how can I programmatically change a button’s text in Python 3.10.4?


Issue 2: Modifying the Button Border in the Layout

I tried modifying the border of a TButton by changing the Button.border element’s border attribute from 1 to 5 using:

style.layout('TButton', [
    ('Button.highlight', {'sticky': 'nswe', 'children': [
        ('Button.border', {'sticky': 'nswe', 'border': '5', 'children': [
            ('Button.padding', {'sticky': 'nswe', 'children': [
                ('Button.label', {'sticky': 'nswe'})
            ]})
        ]})
    ]})
])

However, when I check the layout afterward with style.layout('TButton'), the border value remains 1, as if my change was ignored.

Why doesn’t my modification take effect, and how do I properly update the button’s border in the layout?


Issue 3 : Missing Elements in style.element_names()

When I list all available elements in the current theme using:

style.element_names()

I get:

('uparrow', 'Button.border', 'highlight', 'vsash', 'rightarrow', 'leftarrow', 'hsash', 'arrow', 'downarrow')

However, according to style.layout('TButton'), TButtoncontains elements like Button.highlight and Button.label, but neither of these appears in style.element_names().

Why is Button.border listed explicitly, but not Button.highlight or Button.label? Shouldn't they also be in style.element_names()?


Edit 3 : Summary


I'm very confused about all this. Someone please clear this up for me...

Upvotes: 0

Views: 44

Answers (0)

Related Questions