Python Forum
[Tkinter] Tkinter custom widget styling and creating custom theme - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Tkinter custom widget styling and creating custom theme (/thread-26565.html)



Tkinter custom widget styling and creating custom theme - karolp - May-05-2020

I would like to create a custom theme for my app. I can't get the button widget to expand in my code below. I can configure the button using the minimum width parameter but it should expand to fit the text as specified under layout.
Any ideas what is wrong with the code below?

    import tkinter
    from tkinter import ttk
    root = tkinter.Tk()
    colors = {
              "frame": "#efefef",
              "disabledfg": "#aaaaaa",
              "selectbg": "#657a9e",
              "selectfg": "#ffffff"
             }
    style = ttk.Style()
    style.theme_create("test", "default", settings={
        ".": {
            "configure":
                {"background": colors['frame'],
                 "troughcolor": colors['frame'],
                 "selectbackground": colors['selectbg'],
                 "selectforeground": colors['selectfg'],
                 "fieldbackground": colors['frame'],
                 "font": "TkDefaultFont",
                 "borderwidth": 1},
            "map": {"foreground": [("disabled", colors['disabledfg'])]}
        },
        "TButton": {
                    "configure": {"width": 10, "anchor": "left"},
                    "layout": [
                        ("Button.button", {"children":
                            [("Button.focus", {"children":
                                [("Button.padding", {"children":
                                    [("Button.label", {"side": "left", "expand": 1})]
                                })]
                            })]
                        })
                    ]
                }})
    style.theme_use("test")
    button_send = ttk.Button(root, text="TEST BUTTON ONLY!").grid(row=0, column=0, padx=50, pady=50)
    root.mainloop()



RE: Tkinter custom widget styling and creating custom theme - menator01 - May-05-2020

Take out the width


RE: Tkinter custom widget styling and creating custom theme - karolp - May-05-2020

Hi menator01,

Thanks for your response.
The width is to set up a minimum width of the button in case there is a short word.
This should not affect the button. The widget should expand to fit the text. If you remove the width parameter from the configuration settings you will
find out that the "expand"=1 does not do anything (for example when you change to 0).

If you use ttkthemes module this button will expand to fit the text and the source code for the themes also have the width parameter.
For some reason I can not replicate this when trying to set up my own layout. Probably there is a simple answer to it but cant figure out.

Regards


RE: Tkinter custom widget styling and creating custom theme - menator01 - May-05-2020

Two letters


RE: Tkinter custom widget styling and creating custom theme - karolp - May-05-2020

menator01, in your example the button width also shrunk. This is exactly what I dont want.
I want the button to have a minimum width = 10 and expand automatically if the text is longer than 10.

The source code is here https://github.com/enthought/Python-2.7.3/blob/master/Demo/tkinter/ttk/plastik_theme.py


RE: Tkinter custom widget styling and creating custom theme - menator01 - May-05-2020

I mis-read. use min-width instead of width
Never mind button still shrinks

Per the documents.
Use negative number for min. width
use -10 to get what you want
https://docs.python.org/3/library/tkinter.ttk.html


RE: Tkinter custom widget styling and creating custom theme - karolp - May-06-2020

Thanks for that. It works with the negative value!

However, I still dont understand why when using ttkthemes module the same code works with the positive value?
It seems that "expand" : 1 doesnt do anything? Sorry but I would like really to get into the bottom of this.