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
#1
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()
Reply
#2
Take out the width
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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
Reply
#4
Two letters
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
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....k_theme.py
Reply
#6
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 786 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  neeed to activate a custom tk slider janeik 1 770 Oct-09-2023, 09:29 AM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,664 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,871 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  Creating a function interrupt button tkinter AnotherSam 2 5,412 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [TKINTER] Problems creating directories in a selected path Vulera 2 2,727 Aug-10-2021, 06:38 PM
Last Post: Vulera
  tkinter text widget word wrap position chrisdb 6 7,442 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] Change property of custom Object Sancho_Pansa 0 1,406 Dec-17-2020, 10:53 AM
Last Post: Sancho_Pansa
  How to disable custom button Sancho_Pansa 7 3,411 Dec-04-2020, 02:21 PM
Last Post: buran
Question [PyQt] CSS Styling for a QLabel inside a QListWidget Alfalfa 2 5,046 Nov-30-2020, 02:59 AM
Last Post: Alfalfa

Forum Jump:

User Panel Messages

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