Frame
A frame is a widget that displays just as a simple rectangle. Frames are primarily used as a container for other widgets, which are under the control of a geometry manager such as grid.
Frames are created using the ttk.Frame function:
frame = ttk.Frame(parent)
Requested Size
Padding
frame['padding'] = (5,10)
Borders
To do this,you need to set the "borderwidth" configuration option (which defaults to 0, so no border),as well as the "relief" option, which specifies the visual appearance of the border:"flat" (default), "raised", "sunken", "solid", "ridge", or "groove".
frame['borderwidth'] = 2
frame['relief'] = 'sunken'
Changing Styles
Label
Labels are created using the ttk.Label function, and typically their contents are set up at the same time:
label = ttk.Label(parent, text='Full name:')
Displaying Text
The "text" configuration option shown above when creating the label is the most commonly used, particularly whenthe label is purely decorative or explanatory.
You can also have the widget monitor a variable in your script, so that anytime the variable changes, the label willdisplay the new value of the variable; this is done with the "textvariable" option:
resultsContents = StringVar()
label['textvariable'] = resultsContents
resultsContents.set('New value to display')
Displaying Images
Thisis a two-step process, first creating an image "object", and then telling the label to use that objectvia its "image" configuration option:
image = PhotoImage(file='myimage.gif')
label['image'] = image
You can use both an image and text, as you'll often see in toolbar buttons, via the
"compound" configuration option. The default value is
"none", meaning displayonly the image if present, otherwise the text specified by the
"text" or
"textvariable" options. Other options are
"text" (text only),
"image" (image only),
"center" (text in center of image),
"top" (image above text),
"left",
"bottom", and
"right".
Layout
If the box given to the label is larger than the label requires for its contents, you can usethe "anchor" option to specify what edge or corner the label should be attached to,which would leave any empty space in the opposite edge or corner. Possible values arespecified as compass directions: "n" (north, or top edge), "ne", (north-east, or top right corner),"e", "se", "s", "sw", "w", "nw" or "center".
Labels can be used to display more than one line of text. This can be done by embedding carriagereturns ("\n") in the "text"/"textvariable" string. You can also let the label wrapthe string into multiple lines that are no longer than a given length (with the size specifiedas pixels, centimeters, etc.), by using the "wraplength" option.
You can also control how the text is justified, by using the "justify" option, whichcan have the values "left", "center" or "right". If you only have a single line of text, thisis pretty much the same as just using the "anchor" option, but is more useful withmultiple lines of text.
Fonts, Colors and More
Button
Buttons are created using the ttk.Button function, and typically their contents and command callback are set up at the same time:
button = ttk.Button(parent, text='Okay', command=submitForm)
Text or Image
Buttons take the same "text", "textvariable" (rarely used), "image" and "compound"configuration options as labels, which control whether the button displays text and/or an image.
Buttons have a "default" option, which tells Tk that the button is the default button in the user interface. Set the option to "active" to specify this is a default button; the regular state is "normal".
The Command Callback
button.invoke()
Button State
button.state(['disabled']) ;# set the disabled flag, disabling the button
button.state(['!disabled']) ;# clear the disabled flag
button.instate(['disabled']) ;# return true if the button is disabled, else false
button.instate(['!disabled']) ;# return true if the button is not disabled, else false
button.instate(['!disabled'], cmd) ;# execute 'cmd' if the button is not disabled
Checkbutton
Checkbuttons are created using the ttk.Checkbutton function, and typically set up at the same time:
measureSystem = StringVar()
check = ttk.Checkbutton(parent, text='Use Metric',
command=metricChanged, variable=measureSystem,
onvalue='metric', offvalue='imperial')
check.instate(['alternate'])
Radiobutton
Radiobuttons are created using the ttk.Radiobutton function, and typically as a set:
phone = StringVar()
home = ttk.Radiobutton(parent, text='Home', variable=phone, value='home')
office = ttk.Radiobutton(parent, text='Office', variable=phone, value='office')
cell = ttk.Radiobutton(parent, text='Mobile', variable=phone, value='cell')
Entry
Entries are created using the ttk.Entry function:
username = StringVar()
name = ttk.Entry(parent, textvariable=username)
print('current value is %s' % name.get())
name.delete(0,'end') ; # delete between two indices, 0-based
name.insert(0, 'your name') ; # insert new text at a given index
Passwords
To do this, set the "show" configuration option to the character you'd like to display, e.g. "*".
Widget States
Combobox
Comboboxes are created using the ttk.Combobox function:
countryvar = StringVar()
country = ttk.Combobox(parent, textvariable=countryvar)
country.bind('<<ComboboxSelected>>', function)
Predefined Values
country['values'] = ('USA', 'Canada', 'Australia')
from:
http://www.tkdocs.com/tutorial/widgets.html