6

Hi I am trying to use the ttk Combobox to create a dropdown with options . While doing so i can configure the font size of the default value passed to it . But when i click the arrow the font size of the other values remains the same .I am developing the app for touchscreen , so i need to provide proper size . Heres the sample code , when i run the code the size of A is bigger , button the on clicking the arrow key i see the other values are of default size .

#! /usr/bin/python

from Tkinter import *
import ttk


class Application:

    def __init__(self, parent):
        self.parent = parent
        self.combo()

    def combo(self):
        self.box_value = StringVar()
        self.box = ttk.Combobox(self.parent, textvariable=self.box_value,font=("Helvetica",20))
        self.box['values'] = ('A', 'B', 'C')
        self.box.current(0)
        self.box.grid(column=0, row=0)

if __name__ == '__main__':
    root = Tk()
    app = Application(root)
    root.mainloop()
1
  • This probably a platform specific problem. Which OS are you using?
    – nbro
    Mar 9, 2015 at 10:23

2 Answers 2

12

The thing is that the dropdown menu of the ttk Combobox is actually a simple Tkinter Listbox so it isn't affected by the ttk style. If it would be possible to get a reference to the Listbox from the Combobox, changing the font would be easy. However, I couldn't find a way to do so in Tkinter.

Edited as per patthoyts' very useful comment.
What you can do is change the font for all Listboxes that are part of a Combobox using

bigfont = tkFont.Font(family="Helvetica",size=20)
root.option_add("*TCombobox*Listbox*Font", bigfont)

That changes the font of all Listbox widgets that are part of a ttk Combobox and that are created after calling this.
This does affect all new Comboboxes, but I assume that's what you want. If you want the new font only for this Combobox, you could choose to create this Combobox as the last widget and call self.parent.option_add("*TCombobox*Listbox*Font", bigfont) right before creating this Combobox. Then only the Listbox under this Combobox will have the new font.


If you want all widgets to have the bigger font, you can use

root.option_add("*Font", bigfont)

or you can change the default font as described in this answer.

5
  • 1
    This is basically correct. If you look in the ttk combobox.tcl code you can see that the dropdown listbox fonts set up using option add *TCombobox*Listbox.font TkTextFont so I suggest using the "*TCombobox" prefix to apply changes only to combobox dropdowns.
    – patthoyts
    Mar 9, 2015 at 11:19
  • Great comment, thanks. Now I remember I've seen the *TCombobox*Listbox combination, but I completely overlooked using that to only target Combobox Listboxes. I'll add this into my answer.
    – fhdrsdg
    Mar 9, 2015 at 11:25
  • How can i change the the width of the dropdown arrow key
    – Deepworks
    Mar 10, 2015 at 9:24
  • For as far as I can tell, that's not possible.
    – fhdrsdg
    Mar 10, 2015 at 14:58
  • 1
    root.option_add('*Font', myFont) is fantastic Apr 9, 2015 at 6:42
0

While working on the same issue as the OP, the problem of the arrow size mentioned in the comments of the accepted answer by Deepworks and fhdrsdg came up. Unfortunately I'm new and can't comment, hence I'm posting this as an answer. There is actually a way to set the arrow size via the Style "arrowsize" option.

style = ttk.Style()
style.configure('W.TCombobox',arrowsize = 60)
cBox = ttk.Combobox(self, style='W.TCombobox')

This allows you to increase the arrow size to match the font size of the rest of the widget.

I found the reference to the "arrowsize" option here: Tcl8.6.10/Tk8.6.10 Documentation > Tk Commands > ttk_combobox

3
  • This no longer seems to work. That is, setting the arrowsize from style.configure does not change the size of the arrow button. My version of Tkinter is 8.6, though, so I am suspicious if you ever actually got this to work, or if you just posted here without testing your code.
    – boof
    Aug 7, 2020 at 6:35
  • @boof It definitely works. I tested it before posting and I retested now, still works. I'm also on version 8.6. Send me your code and I'll see if I can get it to work so we can post here Aug 8, 2020 at 19:26
  • Style options may or may not work depending on the ttk theme being used. Try the arrowsize option with a different theme. For example, style.theme_use("clam").
    – 8349697
    Dec 10, 2022 at 12:00

Your Answer

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.