Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox fontsize in tkinter

Tags:

python

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()
like image 323
Deepworks Avatar asked Mar 09 '15 09:03

Deepworks


1 Answers

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.

like image 60
fhdrsdg Avatar answered Oct 03 '22 13:10

fhdrsdg