1

I am wanting to change the borderwidth and relief of a ttk combobox. But I only want to change these attributes for 1 combobox. Is there a way to make the second combobox have a borderwidth of 3 and a relief of solid, like the text box 2?

import tkinter as tk
from tkinter import font as tkfont, filedialog, messagebox
from tkinter.ttk import Combobox

class SLS_v1(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # Setting up the root window
        self.title('APP')
        self.geometry("552x700")
        self.resizable(False, False)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        self.frames["MenuPage"] = MenuPage(parent=container, controller=self)


        self.frames["MenuPage"].grid(row=0, column=0, sticky="nsew")

        self.show_frame("MenuPage")


    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class MenuPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        text1 = tk.Text(self, width=25, height=1)
        text1.pack(pady=20)

        combobox1 = tk.ttk.Combobox(self, width=25, height=2, state='readonly')
        combobox1.pack(pady=20)

        text2 = tk.Text(self, width=50, borderwidth=3, relief='solid', height=1)
        text2.pack(pady=20)

        combobox2 = tk.ttk.Combobox(self, width=50, height=5, state='readonly')
        combobox2.pack(pady=20)


if __name__ == "__main__":
    app = SLS_v1()
    app.mainloop()

1 Answer 1

1

To change ttk objects you must define the style like this.

def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)

    self.style = ttk.Style(self)
    self.style.theme_use("default")

Then you can define specifics for your Combobox like this.

self.style.configure("K.TCombobox",
                     **dict(
                         padding = 1, arrowsize = 12,
                         borderwidth = 3, relief = "solid"))

Complete implementation like this.

combobox2 = Combobox(self, width=50, height=5, state='readonly', style = "K.TCombobox")
2
  • This is partially working. It has allowed me to change some of the features, but it seems like the relief is not working, and I cannot get the background to be black, like the text box.
    – Lzypenguin
    Dec 11, 2021 at 21:27
  • Yes it's strange how relief controls the button surround instead of the Combobox, while borderwidth affects the Combobox.
    – Derek
    Dec 11, 2021 at 22:43

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.