2

Description

I want to make a GUI using Tkinter / ttk with python, the reason is because I want to learn to style GUI's.

I have tried to find information about how to style the "Entry box" not the background but the actual "insert box" but I cant find any information about how to do this and the built in themes is quite well hidden because I cant find those either..

Image demonstration:

  • Default Style

Default Style

  • How I want it

How I want it


My questions

  • Is this even possible and if so, how?
  • And is there a way to access the default themes in order to learn from them?
gofr1
  • 15,741
  • 11
  • 42
  • 52
Estrobeda
  • 25
  • 1
  • 10
  • Tkinter is pretty horrible with that, even with ttk. I didn't figure out a way to do something like you are asking, when I tried to do something similar. This is one reason why I switched my project to Kivy recently. – Matthias Schreiber May 24 '16 at 07:09
  • 1
    Ah ok, I have looked into Kivy but if I can find a way to achieve my goals with tkinter, I would prefere it because I want to use as much standard lib as possible. – Estrobeda May 24 '16 at 07:17

4 Answers4

5

The standard style applied to ttk.Entry simply doesn't take a fieldbackground option, which would be what changes the colour of the text entry field. The solution is this to create a new element that does respond to the option.

from tkinter import *
from tkinter import ttk

root_window = Tk()

estyle = ttk.Style()
estyle.element_create("plain.field", "from", "clam")
estyle.layout("EntryStyle.TEntry",
                   [('Entry.plain.field', {'children': [(
                       'Entry.background', {'children': [(
                           'Entry.padding', {'children': [(
                               'Entry.textarea', {'sticky': 'nswe'})],
                      'sticky': 'nswe'})], 'sticky': 'nswe'})],
                      'border':'2', 'sticky': 'nswe'})])

estyle.configure("EntryStyle.TEntry",
    fieldbackground="light blue")           # Set color here

entry = ttk.Entry(root_window, style="EntryStyle.TEntry")
entry.pack(padx=10, pady=10)

root_window.mainloop()
Stevo Mitric
  • 1,570
  • 18
  • 19
  • 1
    Only thing thats not very clear is how you get access to **"fieldbackground"**, haven't found any documentation about it and it dosn't seem to be in either **"python-path/tcl"** nor in **"python-path/Lib/tkinter"** also ive noticed that you dont seem to need the **"estyle.element_create()"**, it works the same without it..... (dont need to answer furter if you dont want to, Im sure I will find out exactly how it works in dept =D ) – Estrobeda May 26 '16 at 19:45
  • Some time ago i had the same problem as this one, so i just pasted the code here. Don't really remember how i found it, but i think i saw a site explaining how it works (don't have a link though). =P – Stevo Mitric May 27 '16 at 11:03
3

Yes, it is possible, you can make your default themes and assign the widgets these themes. What you're looking for is the Style option.

I learnt pretty much everything I needed to know about styles from this : https://docs.python.org/3/library/tkinter.ttk.html#tkinter.ttk.Style

Here's a small example that should give you the basic idea

import tkinter
from tkinter import ttk

root = tkinter.Tk()
ttk.Style().configure("Blue.TEntry", background="blue")

blue_ent= ttk.Entry(text="Test", style="Blue.TEntry").pack()

root.mainloop()

This gives a good description of how to use ttk.Style() aswell: http://www.tkdocs.com/tutorial/styles.html

Krishna_Sp
  • 111
  • 5
  • True, although ive looked on both sites multiple times but it seems to be possible to change just about anything "Except" the input box. The background changes the color behind the input box etc but none of the options seem to change the actual input box. – Estrobeda May 24 '16 at 10:22
  • Have you tried `foreground` instead of `background` – Krishna_Sp May 24 '16 at 10:31
  • yep but it just changes the "font color" – Estrobeda May 24 '16 at 10:43
  • Hmm, never come across this. I'm not sure if you can change the actual color of the box. – Krishna_Sp May 24 '16 at 10:47
0
import tkinter
from tkinter import ttk

root = tkinter.Tk()
estyle = ttk.Style()
estyle.configure("Blue.TEntry", background="blue", fieldbackground="light blue")
estyle.layout("Blue.TEntry",
                   [('Entry.plain.field', {'children': [(
                       'Entry.background', {'children': [(
                           'Entry.padding', {'children': [(
                               'Entry.textarea', {'sticky': 'nswe'})],
                      'sticky': 'nswe'})], 'sticky': 'nswe'})],
                      'border':'2', 'sticky': 'nswe'})])
blue_ent= ttk.Entry(text="Test", style="Blue.TEntry")
blue_ent.pack()

root.mainloop()
PythonProgrammi
  • 22,305
  • 3
  • 41
  • 34
0

You could always look at CustomTkinter, which is theme oriented.The themes are implemented as JSON files. Creating these is made easy with CTk Theme Builder, which you can find on GitHub, here. It also has few themes included.

Clive Bostock
  • 150
  • 1
  • 11