[Tkinter-discuss] ttk style dilema

Guilherme Polo ggpolo at gmail.com
Mon Apr 27 22:35:59 CEST 2009

On Mon, Apr 27, 2009 at 12:15 PM, Sorin Schwimmer <sxn02 at yahoo.com> wrote:
> Hi All,
> I installed recently tcl/tk 8.5.6, Python 2.6.2 and pyttk 0.3 on a Linux (Gentoo) machine.

Hi Sorin, thanks for giving ttk a try.

> I would like to be able to change two things in entries:
> - the font should be the default, but in italics
> - selected text in an entry should have white background and gray foreground
> How can I accomplish that? How can I determine what is the actual TkDefaultFont?

I will answer in the reverse order.

The easier way to know what defines the actual TkDefaultFont is to use
the tkFont module (it is included with tkinter):

import Tkinter
import tkFont
print tkFont.Font(font='TkDefaultFont').configure()

Now, to change the selection colors in a ttk.Entry, simply do:

import ttk
style = ttk.Style()
style.map('TEntry', selectbackground=[('!disabled', 'white')],
selectforeground=[('!disabled', 'grey')])

And to change the ttk.Entry font to italics you don't use themes
actually. There is an option named 'font' for this widget, so you just
have to change it. Follows the correct way to do what you are after:

import ttk
import tkFont

entry = ttk.Entry()
myfont = tkFont.Font(font=str(entry['font']))
myfont['slant'] = 'italic'
entry['font'] = myfont

> Thanks
> SxN


-- Guilherme H. Polo Goncalves

More information about the Tkinter-discuss mailing list