Welcome, guest | Sign In | My Account | Store | Cart

This trick shows how to create a bordered frame with different border size in each side.

Python, 34 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Author: Miguel Martinez Lopez

try:
    from Tkinter import Tk, Frame
except ImportError:
    from tkinter import Tk, Frame

class Bordered_Frame(Frame):
    def __init__(self, master, bordercolor=None, borderleft=0, bordertop=0, borderright=0, borderbottom=0, interiorwidget=Frame, **kwargs):
        Frame.__init__(self, master, background=bordercolor, bd=0, highlightthickness=0)

        self.interior = interiorwidget(self, **kwargs)
        self.interior.pack(padx=(borderleft, borderright), pady=(bordertop, borderbottom))
        
        
if __name__ == "__main__":
    try:
        from Tkinter import Tk, Label
    except ImportError:
        from tkinter import Tk, Label
    
    root = Tk()
    root.geometry("300x400")
    root.configure(background="white")

    f = Bordered_Frame(root, text="This is a text", background="white", bordercolor="blue", padx=3, borderleft=7, interiorwidget=Label)
    f.pack(pady=10)
    
    f = Bordered_Frame(root, background="white", bordercolor="green", borderleft=7, bordertop=2, borderright=2, borderbottom=2)
    f.pack(pady=10)

    Label(f.interior, text="This is another example", background="white").pack(padx=4, pady=2)

    root.mainloop()