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

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)
app.mainloop()

<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


argumentusagedesc
anchoranchor=otherAnchor must be a valid anchor position such as n or sw; it specifies where to position each content in its parcel. Defaults to center.
bordermodebordermode=mapwhether to take border width of master widget into account
outside : includes parent's border size
inside : excludes  parent's border size

widthwidth=amountResize the widget.
heightheight=amountResize the widget.
relheightrelheight=amountResize the widget to a relative size based on the size of the parent window.
relwidthrelwidth=amount
Resize the widget to a relative size based on the size of the parent window.
xx=amountSpecifies the location of the widget.
yy=amountSpecifies the location of the widget.
relxrelx=amountSpecifies the widget's position relative to the size of the parent window.
relyrely=amount
Specifies the widget's position relative to the size of the parent window.




anchor


There are some things to be careful about, as shown in the following picture. When applying an anchor in the area allocated in placed, the entire area of the widget may not be drawn.


As shown in the second picture, if the anchor is se(south, east), the widget size will exceed the area specified by place. The size of the frame widget is 150

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>




















댓글

이 블로그의 인기 게시물

MQTT - C/C++ Client

RabbitMQ - C++ Client #1 : Installing C/C++ Libraries

C/C++ - Everything about time, date