简体   繁体   中英

How to add a border for a frame in Python Tkinter

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?

边框如图所示

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 info available in documentation: link

I request you to read the documentation thoroughly, and 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)
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 used to change the color of the widget in focus

-> highlightthickness used to specify the thickness of the border around the widget in focus.

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM