Python - ttkbootstrap widgets layout #2 place
place allows you to position a widget by specifying exact location coordinates.
Absolute coordinates (x, y) can be used, and a relative ratio to the parent window size (relx, rely) can also be used.
import ttkbootstrap as tb app = tb.Window() app.style.theme_use("superhero") app.geometry("750x450")app.mainloop()frame = tb.Frame(app, bootstyle="primary", width=100, height=100)frame.place(x = 100, y = 100)frame2 = tb.Frame(app, bootstyle="warning", width=100, height=100)frame2.place(x = 300, y =100)
<basic_place.py>
def place( cnf: Mapping[str, Any] | None = {}, *, anchor: _Anchor = ..., bordermode: Literal['inside', 'outside', 'ignore'] = ..., width: _ScreenUnits = ..., height: _ScreenUnits = ..., x: _ScreenUnits = ..., y: _ScreenUnits = ..., relheight: str | float = ..., relwidth: str | float = ..., relx: str | float = ..., rely: str | float = ...,
in_: Misc = ...,
**kw: Any
)
Place a widget in the parent widget. Use as options: in=master - master relative to which the widget is placed in_=master - see 'in' option description x=amount - locate anchor of this widget at position x of master y=amount - locate anchor of this widget at position y of master relx=amount - locate anchor of this widget between 0.0 and 1.0 relative to width of master (1.0 is right edge)
rely=amount - locate anchor of this widget between 0.0 and 1.0 relative to height of master (1.0 is bottom edge)
anchor=NSEW (or subset) - position anchor according to given direction width=amount - width of this widget in pixel height=amount - height of this widget in pixel relwidth=amount - width of this widget between 0.0 and 1.0 relative to width of master (1.0 is the same width as the master)
relheight=amount - height of this widget between 0.0 and 1.0 relative to height of master (1.0 is the same height as the master)
bordermode="inside" or "outside" - whether to take border width of master widget into account
argument | usage | desc |
---|---|---|
anchor | anchor=other | Anchor must be a valid anchor position such as n or sw; it specifies where to position each content in its parcel. Defaults to center. |
bordermode | bordermode=map | whether to take border width of master widget into account outside : includes parent's border size inside : excludes parent's border size |
width | width=amount | Resize the widget. |
height | height=amount | Resize the widget. |
relheight | relheight=amount | Resize the widget to a relative size based on the size of the parent window. |
relwidth | relwidth=amount | Resize the widget to a relative size based on the size of the parent window. |
x | x=amount | Specifies the location of the widget. |
y | y=amount | Specifies the location of the widget. |
relx | relx=amount | Specifies the widget's position relative to the size of the parent window. |
rely | rely=amount | Specifies the widget's position relative to the size of the parent window. |
anchor
Similarly, when using coordinates (x, y) or relative coordinates, two widgets may overlap.
import ttkbootstrap as tb app = tb.Window() app.style.theme_use("superhero") app.geometry("750x450") frame = tb.Frame(app, bootstyle="primary", width=150, height=150) frame.place( anchor='nw', x = 150, y = 150) frame2 = tb.Frame(app, bootstyle="warning", width=150, height=150) frame2.place(anchor='se', x = 350, y =350) app.mainloop()
<overlap_place.py>
bordermode
bordermode specifies whether to consider the border thickness of the parent window when calculating the widget's position coordinates.
import ttkbootstrap as tb app = tb.Window() app.style.theme_use("superhero") app.geometry("750x450") mainframe = tb.Frame(app, bootstyle="success", width=150, height=150, borderwidth=50) mainframe.pack(expand=True, fill="both") frame = tb.Frame(mainframe, bootstyle="primary", width=100, height=100) frame.place( anchor='nw', x = 150, y = 100, bordermode='outside') frame2 = tb.Frame(mainframe, bootstyle="warning", width=100, height=100) frame2.place(anchor='nw', x = 150, y = 200, bordermode='inside') app.mainloop()
<border_place.py>
댓글
댓글 쓰기