To control the appearance of a widget, you usually use options rather than method calls. Typical options include text and color, size, command callbacks, etc. To deal with options, all core widgets implement the same configuration interface:
Syntax:
config(option=value, …)
configure(option=value, …)
Example:
from Tkinter import *
root = Tk()
labelfont = ('times', 20, 'bold') # family, size, style
widget = Label(root, text='Hello config world')
widget.config(bg='black', fg='yellow') # yellow text on black label
widget.config(font=labelfont) # use a larger font
widget.config(height=3, width=20) # initial size: lines,chars
widget.pack(expand=YES, fill=BOTH)
root.mainloop()
Widget Styling
All Tkinter standard widgets provide a basic set of “styling” options, which allow you to modify things like colors, fonts, and other visual aspects of each widget.
Colors
By setting the bg option of the label widget here, its background is displayed in black; the fg option similarly changes the foreground (text) color of the widget to yellow. These color options work on most Tkinter widgets, and accept either a simple color name (e.g., 'blue') or a hexadecimal string. Most of the color names you are familiar with are supported (unless you happen to work for Crayola). You can also pass a hexadecimal color identifier string to these options to be more specific; they start with a # and name a color by its red, green, and blue saturations, with an equal number of bits in the string for each. For instance, '#ff0000' specifies eight bits per color, and defines pure red -- "f" means four "1" bits in hexadecimal. We'll come back to this hex form when we meet the color selection dialog later in this chapter.
Color Names
Tkinter includes a color database which maps color names to the corresponding RGB values. This database includes common names like Red, Green, Blue, Yellow, and LightBlue, but also more exotic things like Moccasin, PeachPuff, etc.
Fonts
Fonts are usually specifed using the font widget option. Tkinter supports a number of different font descriptor types:
Font descriptors
User-defined font names
System fonts
X font descriptors
Examples:
("Times", 10, "bold")
("Helvetica", 10, "bold italic")
("Symbol", 8)
Example:
from tkinter import *from tkinter.ttk import *# Set the canvas
canv = Tk()
canv.geometry('200x150')#Create style object
sto = Style()#configure style
sto.configure('W.TButton', font= ('Arial', 10, 'underline'),
foreground='Green')#Button with style
btns = Button(canv, text='Welcome !',
style='W.TButton',
command=canv.destroy)
btns.grid(row=0, column=1, padx=50)#Button without style
btnns = Button(canv, text='Click to Start !', command=None)
btnns.grid(row = 1, column = 1, pady = 10, padx = 50)
canv.mainloop()
The Font constructor supports the following style options (note that the constants are defined in the tkFont module):
family
Font family.
size
Font size in points. To give the size in pixels, use a negative value.
weight
Font thickness. Use one of NORMAL or BOLD. Default is NORMAL.
slant
Font slant. Use one of NORMAL or ITALIC. Default is NORMAL.
underline
Font underlining. If 1 (true), the font is underlined. Default is 0 (false).
overstrike
Font strikeout. If 1 (true), a line is drawn over text written with this font. Default is 0 (false).
Text Formatting
While text labels and buttons usually contain a single line of text, Tkinter also supports multiple lines. To split the text across lines, simply insert newline characters (\n) where necessary.
By default, the lines are centered. You can change this by setting the justify option to LEFT or RIGHT. The default value is CENTER.
Other Anchor attribute which used for text formatting:
NW
N
NE
W
CENTER
E
SW
S
SE
How to use these anchor attribute:
Label(root, text='Pack', anchor='w').pack(fill='both')