Python Tkinter中place()方法bordermode属性:OUTSIDE与INSIDE的区别
Alright, let's demystify the bordermode attribute in Tkinter's place() layout method—this is one of those small but impactful details that can trip you up if you don't get the difference between its two values.
First, a quick overview: bordermode controls which part of your widget is used as the reference point when positioning it with place(). Specifically, it determines whether the widget's outer border edges or its inner content area edges are used to calculate x/y coordinates or relative positioning values like relx/rely.
bordermode=OUTSIDE (Default Value)
This is the setting Tkinter uses if you don't explicitly define bordermode. Here's what it does:
- Positioning calculations are based on the outermost edges of the widget (including any border width set with
bdorborderwidth). - When you set
x=10, y=10, the top-left corner of the widget's outer border will sit at (10,10) relative to its parent container. - Example: If you have a button with
bd=8, the clickable content area of the button will start 8 pixels inside that (10,10) point.
bordermode=INSIDE
This setting flips the reference point to the widget's inner content area:
- Positioning calculations ignore the widget's border entirely, using the top-left corner of the content area as the reference.
- When you set
x=10, y=10, the top-left corner of the widget's actual content (text/icon) will sit at (10,10)—meaning the widget's outer border will extend outward from that point (potentially overlapping the parent container's edges if the border is thick enough). - Example: Using the same
bd=8button, the outer border will start at (2,2) (10-8, 10-8), so the left and top edges of the border will be outside the parent's visible area if the parent starts at (0,0).
Quick Code Example to See the Difference
import tkinter as tk root = tk.Tk() root.geometry("300x200") root.configure(bg="#f0f0f0") # Default OUTSIDE mode btn_outside = tk.Button( root, text="bordermode=OUTSIDE", bd=10, bg="lightblue", padx=5, pady=5 ) btn_outside.place(x=30, y=30, bordermode=tk.OUTSIDE) # INSIDE mode btn_inside = tk.Button( root, text="bordermode=INSIDE", bd=10, bg="lightgreen", padx=5, pady=5 ) btn_inside.place(x=30, y=100, bordermode=tk.INSIDE) root.mainloop()
Run this code, and you'll notice:
- The blue button's outer border is neatly aligned with the (30,30) position.
- The green button's text starts at (30,100), so its thick border hangs off the left and top sides of that position.
Key Takeaways
- Use
bordermode=OUTSIDEwhen you want the entire widget (border included) to be positioned relative to the parent's coordinates. - Use
bordermode=INSIDEwhen you want to position the content area of the widget independently of its border—useful if you need consistent alignment of text/icons regardless of border size.
内容的提问来源于stack exchange,提问作者Nehal Birla

