1

To set the style for the ttk.Combobox, I can do something like this:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

combostyle = ttk.Style()

combostyle.theme_create('combostyle', parent='alt',
                         settings = {'TCombobox':
                                     {'configure':
                                      {'selectbackground': 'blue',
                                       'fieldbackground': 'red',
                                       'background': 'green'
                                       }}}
                         )
combostyle.theme_use('combostyle') 

combo = ttk.Combobox(root, values=['1', '2', '3'])
combo['state'] = 'readonly'
combo.pack()

entry = tk.Entry(root)
entry.pack()

root.mainloop()

But that sets the theme for all tkinter and ttk widgets. I want to set the style for only the Combobox. How can I do this?

I am using Python 3 on Windows 10.

Any help is greatly appreciated.

Andereoo
  • 834
  • 1
  • 8
  • 24
  • ```combostyle.configure('MyCustomStyleName.TCombobox', selectbackground = 'blue', ........)``` ```combo = ttk.Combobox(root, values=['1', '2', '3'], style = 'MyCustomStyleName.TCombobox')``` – SRR Apr 04 '20 at 21:30
  • Thanks! How do I change the background? – Andereoo Apr 04 '20 at 21:49
  • Add background='red' or background='HexColourCode'. Did my snippet work btw? – SRR Apr 04 '20 at 22:00
  • Your first snippet worked... But... I need to change the background of the combobox, too. Setting `background='red'` did nothing. – Andereoo Apr 04 '20 at 22:55
  • combostyle.configure('MyCustomStyleName.TCombobox',fieldbackground=[('readonly','red')], selectbackground=[('readonly', 'white')],selectforeground=[('readonly', 'black')] . If that works I'll post a proper answer – SRR Apr 04 '20 at 23:08
  • That code didn't do anything. Sorry. – Andereoo Apr 04 '20 at 23:31
  • Looks like you'll have to manually map it. Check out this post on how to to that: https://en.it1352.com/article/782f4be618cf49639217c36ab06fec6a.html – SRR Apr 04 '20 at 23:33

1 Answers1

1

Your original method configures a global theme. To get a theme to be attached to one component you have to create it and attach it to the widget class.

combostyle.configure('MyCustomStyleName.TCombobox', selectbackground = 'blue', ........) 
combo = ttk.Combobox(root, values=['1', '2', '3'], style = 'MyCustomStyleName.TCombobox')
SRR
  • 1,608
  • 1
  • 21
  • 51
  • This isn't doing anything for me. – Andereoo May 06 '20 at 14:01
  • ...I'm slightly confused, in the comments of your original post you said it worked – SRR May 06 '20 at 16:01
  • ```selectbackground``` and ```foreground worked```. ```background``` and ```fieldbackground``` (the remaining two commands in my original code) didn't, which are what I really care about. My previous comments noted this. – Andereoo May 06 '20 at 17:40
  • I found the answer here: https://stackoverflow.com/questions/50834356/ttk-combobox-set-background-colour-programatically-and-dynamically?rq=1. It turns out that I have to use the ```alt``` or ```clam``` themes to customize the background. If you can update your answer, I'll mark it as the correct answer. – Andereoo May 06 '20 at 17:49