Piratz Label#
First create our image, this invokes a Caribbean island, the palm tree poses a challenge, particularly if the label grows in height. Choose border sizes that give the desired effect, then test using theme_create or theme_settings rather than styling (individual widget with configure, layout and map).
Increase the height of the widget by using two lines of text - certainly easier than adding a configure clause.
Try changing the border size to [20, 6, 4, 4], it looks reasonable if we have sticky "ew" and only one line of code, however let's keep it suitable for more than one line of text and change back to the original border size [17, 9, 7, 3] and sticky "news".
The padding [17,5,3,3] is required to position the text.
Piratz Label with white Surround#
The surround has been left white to show the grid.
Having created the image it is relatively easy to make it grey in our image editor and save the image for the disabled state.
The text area has been made transparent, in fact the appearance may look better without a white surround, so we'll make the surround transparent as well.
Run the script and see whether the states look right. As you see we can test more than one widget and prove that the settings and widgets operate as expected.
Show/Hide Code 07pirate_label.py
'''
Used as template for other widgets in the theme
Create theme extract for custom widgets, include state selection to view
the result of changing the state using different images and/or different
settings.
Selection of border and padding a little tricky try the border with
[20, 6, 4, 4] with/without padding, padding positions the text
also experiment with sticky as "ew".
'''
from tkinter import Tk, PhotoImage
from tkinter.ttk import Style, Label, Frame
from RunState import run_state
root = Tk()
img1 = PhotoImage("label", file='../images/piratz/label.png')
img2 = PhotoImage("label-d", file='../images/piratz/label-d.png')
style = Style()
# both theme_create and theme_settings worked
style.theme_create( "yummy", parent="clam", settings={
#style.theme_settings('default', {
# start of theme extract
'Label.border': {"element create":
('image', "label",
('disabled', "label-d"),
{'border':[17, 9, 3, 7], 'padding':[17,5,3,3], 'sticky': "nsew"})
}
# end of theme extract - don't forget to add comma at end when inserting
})
style.theme_use('yummy') # 'default'
fr = Frame(root)
fr.grid(column=0,row=0,sticky='nsew')
widg = Label(fr,text='Piratz! make it long')
widg.grid(column=0,row=13,sticky='nsew', padx=5, pady=5)
widg1 = Label(fr,text='Piratz!\nextra line')
widg1.grid(column=0,row=14,sticky='nsew', padx=5, pady=5)
run_state(fr,widg,widg1)
root.mainloop()