Python Forum
[Tkinter] Tkinter custom widget styling and creating custom theme
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter custom widget styling and creating custom theme
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={
        ".": {
                {"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})]
    button_send = ttk.Button(root, text="TEST BUTTON ONLY!").grid(row=0, column=0, padx=50, pady=50)
Take out the width
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.

Two letters
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
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
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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Change property of custom Object Sancho_Pansa 0 103 Dec-17-2020, 10:53 AM
Last Post: Sancho_Pansa
  How to disable custom button Sancho_Pansa 7 249 Dec-04-2020, 02:21 PM
Last Post: buran
Question [PyQt] CSS Styling for a QLabel inside a QListWidget Alfalfa 2 203 Nov-30-2020, 02:59 AM
Last Post: Alfalfa
  Tkinter - How can I extend a label widget? TurboC 2 299 Oct-13-2020, 12:15 PM
Last Post: zazas321
  [Tkinter] How to Print a list = ['a','b','c'], using tkinter along with a custom font? Pleiades 2 307 Sep-15-2020, 03:54 PM
Last Post: Pleiades
  Tkinter calendar widget scratchmyhead 4 1,162 May-03-2020, 07:01 PM
Last Post: scratchmyhead
  Creating and destroying dynamic labels in Tkinter MarcusRoberts 1 700 May-02-2020, 06:49 PM
Last Post: Yoriz
  [Tkinter] Problem with tkinter when creating .exe file Jan_97 2 1,438 Feb-27-2020, 05:17 PM
Last Post: Jan_97
  [PyGUI] Python 3.8.1 Tkinter Widget stete change sasiap 4 581 Feb-14-2020, 10:38 AM
Last Post: sasiap
  [PyQt] dynamically resize custom widget fill remaining space ironcthulhu 1 1,491 Dec-31-2019, 09:45 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020