Deepworks
Deepworks

Reputation: 401

Combobox fontsize in tkinter

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()

Upvotes: 7

Views: 14296

Answers (2)

fhdrsdg
fhdrsdg

Reputation: 10602

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.

Upvotes: 14

SuzukiBKing
SuzukiBKing

Reputation: 178

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

Upvotes: 0

Related Questions