__init__( self, master, layout="auto", grid=None, align=None, visible=True, enabled=None, width=None, height=None, border=None)
What is it?
Box object is an invisible container which can contain other widgets. It is the only object other than
App which can act as the
master for other objects and can have its own layout manager.
You can use the
Box object to group other objects within your GUI.
How do I make one?
Box object like this:
from guizero import App, Box app = App() box = Box(app) app.display()
When you create a
Box object you must specify a master, and you can specify any of the optional parameters. Specify parameters in the brackets like this:
box = Box(app, layout="grid")
|master||App, Window or Box||-||Yes||The container to which this widget belongs|
|align||string||None||-||Alignment of this widget within its container. Possible values:
|layout||string||"auto"||-||Whether widgets inside this box pack themselves (
|visible||boolean||True||No||If the widget should be visible.|
|enabled||boolean||None||No||If the widget should be enabled. If
|width||size||None||No||Set the width of the widget in pixels or to
|height||size||None||No||Set the height of the widget in pixels or to
|border||int||None||No||Sets the border thickness.
You can call the following methods on a
|add_tk_widget(tk_widget, grid=None, align=None, visible=True, enabled=None, width=None, height=None)||tk_widget (tk), grid (list), align (str), visible (bool), enabled (bool), width (int), height (int)||Widget||Adds a tk widget into a guizero container. Note - this is an advanced feature see Using tk for more information.|
|after(time, command, args=None)||time (int), command (function name), args (list of arguments)||-||Schedules a single call to
|cancel(command)||command (function name)||-||Cancels a scheduled call to
|disable()||-||-||Disables all the widgets in the box so that they cannot be interacted with|
|destroy()||-||-||Destroys the widget|
|enable()||-||-||Enables all the widgets in the box|
|focus()||-||-||Gives focus to the widget (e.g. focusing a
|hide()||-||-||Hides the widget from view. This method will unpack the widget from the layout manager.|
|repeat(time, command, args=None)||time (int), command (function name), args (list of arguments)||-||Repeats
|resize(width, height)||width (int), height (int)||-||Sets the width and height of the widget|
|set_border(thickness, color)||thickness (int), color (color)||-||Sets the border thickness and color. Setting thickness to
|show()||-||-||Displays the widget if it was previously hidden|
You can set and get the following properties:
|align||string||The alignment of this widget within its container|
|border||int||The border thickness, setting to
|bg||color||The background colour of the widget|
|font||string||The font that widgets should use|
|height||size||Set the height of the widget in pixels or to
|layout||string||The layout being used by the Box (
|text_size||int||The size of the text widgets should use|
|text_color||color||The colour of the text widgets should use|
|visible||boolean||If this widget is visible|
|width||size||Set the width of the widget in pixels or to
Putting widgets in a Box
Box object is invisible, but it can contain other widgets. In this example, there are two
Text objects. One has
box as its master, the other has
app as its master.
from guizero import App, Box, Text app = App(title="My app", height=300, width=400) box = Box(app) text1 = Text(box, text="Hello from the box", size=14, text_color="red", font="Arial") text2 = Text(app, text="Hello from the app", size=14, text_color="blue", font="Courier New") app.display()
Grouping objects within a Box
It is useful to put objects in a box to group them together. For example here we have given the
app a grid layout, then placed some text at [0,0] and the
Box object at [1,0]. This means that the text will appear on the left, and the contents of the Box will appear on the right.
Box object itself has a grid layout and contains six buttons which are positioned on a separate grid layout belonging to the box.
from guizero import App, Text, Box, PushButton def do_nothing(): return 0 app = App(title="My app", height=300, width=300, layout="grid") text = Text(app, text="Some text here", grid=[0,0]) box = Box(app, layout="grid", grid=[1,0]) button1 = PushButton(box, command=do_nothing, text="1", grid=[0,0]) button2 = PushButton(box, command=do_nothing, text="2", grid=[1,0]) button3 = PushButton(box, command=do_nothing, text="3", grid=[2,0]) button4 = PushButton(box, command=do_nothing, text="4", grid=[0,1]) button5 = PushButton(box, command=do_nothing, text="5", grid=[1,1]) button6 = PushButton(box, command=do_nothing, text="6", grid=[2,1]) app.display()