[Tkinter-discuss] Techniques for creating the appearance of frames with custom border colors

Michael Lange klappnase at web.de
Fri Dec 17 15:49:58 CET 2010


Thus spoketh python at bdurham.com 
unto us on Fri, 17 Dec 2010 09:00:08 -0500:

> I understand that Tkinter frames do not have a property that
> allows their border color to be customized.
> 
> Here are some high level ideas I have on how to create a colored
> border effect - any suggestions on best practice appreciated.
> 
> 1. Turn off the frame's border. Enclose the frame in a parent
> frame also without a border. Set the parent frame's background
> color to the color of the border you want. Make sure the parent
> frame's layout options (grid/pack) match the inner frame's
> options.
> 
> 2. Use a canvas instead of a frame, draw a border using the
> create_rectangle( ..., fill="<frame backgroundcolor>" ), and then
> bind to the canvas's <Config>(?) event and to delete and redraw
> the border every time the canvas resizes.
> 
> 3. Use a tk.call( ... ) to gain access to the TCL interpreter and
> its richer set of capabilies?
> 
> Regarding options 2 and 3: Might there be a way to create
> non-solid border styles, eg. borders that are composed of dots or
> dashes?

I don't think there are such options in Tk by default, so 3. probably is
not an option (unless you do very fancy things with ttk styles, but these
are probably also possible with the ttk module).
With a canvas you can try something like:

###################
from Tkinter import *
root = Tk()
root.geometry('500x500')

c = Canvas(root, bg='yellow', bd=0, highlightthickness=0, takefocus=0)
c.pack(fill='both', expand=1, padx=100, pady=100)
c.update_idletasks()
w, h = c.winfo_width(), c.winfo_height()
r = c.create_rectangle(0, 0, w-1, h-1, outline='red', fill='', dash=(5,5))
f = Frame(c, width=w-2, height=h-2)
w = c.create_window(1, 1, window=f, anchor='nw')

def foo(event):
    w, h = event.width, event.height
    event.widget.coords(r, 0, 0, w-1, h-1)
    f.configure(width=w-2, height=h-2)
c.bind('<Configure>', foo)

root.mainloop()
###################

This seems to werk ok, I'm not sure if this might break in some
situations though.

Regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

The only solution is ... a balance of power.  We arm our side with exactly
that much more.  A balance of power -- the trickiest, most difficult,
dirtiest game of them all.  But the only one that preserves both sides.
		-- Kirk, "A Private Little War", stardate 4211.8


More information about the Tkinter-discuss mailing list