12

Below is my UI. I am trying to add a border for the frame.

But I am not getting any information on how to add a border. How can I do it?

Border as in the image

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

5 Answers5

22

The requested feature is not called a border in Tkinter. It is called a highlight.

To get the above request, set highlightbackground="black" and highlightthickness=1.

(The border is the empty space reserved around the frame) Additional information is available in documentation.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tyr52
  • 321
  • 1
  • 3
  • 5
  • 4
    The doc link (at the effbot site) appears to be not viable: "effbot.org is taking a break. We’ll be back, in some form or another." These two options could use better explanation -- i.e. which widget are these options added? Using a frame I get _tkinter.TclError: unknown option "-highlightbackground". I'm requesting since this "answer" appears to be the most upvoted for this question. – Harlin Jan 09 '21 at 23:02
  • 1
    I have to agree with Harlin, this doesn't appear to work for me at all. I think this answer needs updating – Mi-krater Sep 28 '21 at 23:05
6

In the documentation, look at the styles you can apply to a frame using Tkinter: Tkinter Frame Widget

Here is how you do this:

import tkinter as tk
#tk.Frame(master, **config-options)

my_frame = tk.Frame(parent_widget, borderwidth = 1)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
5
from tkinter import *
root = Tk()
frame1 = Frame(root, highlightbackground="blue", highlightthickness=1,width=600, height=100, bd= 0)
frame1.pack()
root.mainloop()
  • change the options accordingly

  • highlightbackground is used to change the color of the widget in focus

  • highlightthickness is used to specify the thickness of the border around the widget in focus.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ishaan Joshi
  • 49
  • 1
  • 4
0

You can use relief and borderwidth like this.

from tkinter import *    
root = Tk()
frame1 = Frame(root, width=400, height=660, bg="White", borderwidth=1, relief=RIDGE)
frame1.place(relx=0.7, y=80)
root.mainloop()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Do they have a name in Python? Named parameters? Perhaps add (non-naked) specific links to *documentation* for those two? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Feb 23 '22 at 17:41
0

I have tried like:

frame_left = tk.Frame(window, width=100, height=360, bg="blue", borderwidth=1, relief=tk.RIDGE)
frame_left.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
frame_right = tk.Frame(window, highlightbackground="green", highlightthickness=10, width=100, height=100, bd=0)
frame_right.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1)

See the link below to for what the output of the above code looks like:

https://i.stack.imgur.com/JkgAm.png

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PlutoSenthil
  • 332
  • 6
  • 13