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

python at bdurham.com python at bdurham.com
Fri Dec 17 18:04:18 CET 2010


Your code works wonderfully - thank you very much!!

One question: Why did you add your inner frame to the canvas as a window
via c.create_window() vs. just packing the frame?

I tried the frame packing technique per the following code change
(replacing c.createwindow with pack):

f = Frame(c, width=w-2, height=h-2)
# w = c.create_window(1, 1, window=f, anchor='nw')
f.pack() # note: will not work if passed fill='both', expand=1

And the code seemed to work with the following caveats:

- the top row of the rectangle is not displayed (I can't figure this one
- screen redraws seem (subjectively) to be less efficient (yellow
background bleeds through)


From: "Michael Lange" <klappnase at web.de>

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
are probably also possible with the ttk module).
With a canvas you can try something like:

from Tkinter import *
root = Tk()

c = Canvas(root, bg='yellow', bd=0, highlightthickness=0, takefocus=0)
c.pack(fill='both', expand=1, padx=100, pady=100)
w, h = c.winfo_width(), c.winfo_height()
r = c.create_rectangle(0, 0, w-1, h-1, outline='red', fill='',
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)


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

More information about the Tkinter-discuss mailing list