[Python] TTK - Don't apply current style for single widget (in own dialog window)

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following problem. In my application I use 'black' theme from ttkthemes, with some additional changes. I also uses dialogs for getting values. I also have created my own dialog with ttk.Combobox widget to allow user to choose a value instead of typing it. The standard dialog uses default style (as it's tk based, not ttk) which is totally OK for me. However in my own dialog, the ttk.Combobox widget uses 'black' theme, when the rest of window uses default one. It doesn't look good :(. A dialog window uses default theme, when a Combobox in this window uses custom theme

    What can I do to use 'default' style for the ttk.Combobox in dialog window, when the other comboboxes in main window are still using my style?

    An example program with parts of my code below

    import tkinter as tk
    from tkinter import simpledialog
    from tkinter import ttk
    from ttkthemes import ThemedStyle


    class ComboDialog(tk.simpledialog.Dialog):
    def __init__(self, title, prompt, values, initial_value = None, parent = None):
    self.__prompt = prompt
    self.value = None
    self.__current_var = tk.StringVar()
    self.__current_var.set('' if initial_value is None else initial_value)
    self.__values = values
    self.__state = 'readonly'
    super().__init__(parent, title)

    def body(self, frame):
    tk.Label(frame, text=self.__prompt, justify=tk.LEFT).grid(row=0, padx=5, sticky=tk.W)
    # The combobox below is in dialog window, so should have 'default' style
    self.__entry = ttk.Combobox(frame, state=self.__state, textvariable=self.__current_var)
    self.__entry.grid(row=1, padx=5, sticky=tk.W+tk.E)
    self.__entry['values'] = self.__values
    return self.__entry

    def apply(self, event=None):
    self.value = self.__entry.get()

    @staticmethod
    def asklist(title, text, values_list : list[str], initial_value : str | None = None, parent = None) -> str:
    dialog = ComboDialog(title, text, values_list, initial_value = initial_value, parent = parent)
    return dialog.value


    # Example program
    tk_root = tk.Tk()
    style = ThemedStyle(tk_root)
    style.set_theme('black')
    background = style.lookup("TLabel", "background")
    tk_root.configure(bg=background)
    frame = ttk.Frame(tk_root)
    # The combobox below is in main window, so should have 'black' style
    ttk.Combobox(frame, values=['This', 'is', 'a', 'test'], state='readonly').pack(padx=5, pady=5)
    ttk.Button(frame, text='Test', command=lambda: ComboDialog.asklist('Test','Select',['abecadlo','z pieca','spadlo'], 'abecadlo')).pack(padx=5, pady=5)
    frame.pack(padx=5, pady=5, fill=tk.BOTH)
    tk_root.mainloop()

    Continue reading...

Compartilhe esta Página