pyTermTk - LayoutsπΆοΈ
IntroπΆοΈ
TTkLayouts are specialised classes that allow the placement of the widgets.
TTkLayoutπΆοΈ
TTkLayout
ββββββββββββββββββββββββββββββ
β pos(4,2) β
β βββββββββ pos(16,4) β
β βWidget1β βββββββββββ β
β β β β Widget2 β β
β β β βββββββββββ β
β β β β
β βββββββββ β
β β
ββββββββββββββββββββββββββββββ
TTkHBoxLayoutπΆοΈ
This layout allow an automatic place all the widgets horizontally
TTkHBoxLayout
βββββββββββ€ββββββββββ€ββββββββββ
β Widget1 β Widget2 β Widget3 β
β β β β
β β β β
β β β β
β β β β
β β β β
βββββββββββ§ββββββββββ§ββββββββββ
TTkVBoxLayoutπΆοΈ
This layout allow an automatic place all the widgets vertically
TTkVBoxLayout
βββββββββββββββββββββββββββββββ
β Widget 1 β
βββββββββββββββββββββββββββββββ’
β Widget 2 β
βββββββββββββββββββββββββββββββ’
β Widget 3 β
βββββββββββββββββββββββββββββββ’
β Widget 4 β
βββββββββββββββββββββββββββββββ
TTkGridLayoutπΆοΈ
This layout allow an automatic place all the widgets in a grid the empty rows/cols are resized to the βcolumnMinHeight,columnMinWidthβ parameters
TTkGridLayout ββ columnMinWidth
βββββββββββ€ββββββββββ€β€ββββββββββ
β Widget1 β Widget2 ββ Widget3 β
β (0,0) β (0,1) ββ (0,3) β
βββββββββββΌββββββββββΌβΌββββββββββ’ β columnMinHeight
βββββββββββΌββββββββββΌβΌββββββββββ’ β
β Widget4 β ββ β
β (2,0) β ββ β
βββββββββββΌββββββββββΌβΌββββββββββ’
β β ββ Widget5 β
β β ββ (3,3) β
βββββββββββ§ββββββββββ§β§ββββββββββ
ExamplesπΆοΈ
1 - Simple TTkLayoutπΆοΈ
Following is the code to execute Layout Example in pyTermTk (tryItOnline)
import TermTk as ttk
# TTkLayout is used by default
root = ttk.TTk()
# Attach 4 buttons to the root widget
ttk.TTkButton(parent=root, pos=(0,0), size=(15,5), border=True, text="Button1")
ttk.TTkButton(parent=root, pos=(0,5), size=(10,4), border=True, text="Button2")
ttk.TTkButton(parent=root, pos=(10,6), size=(10,3), border=True, text="Button3")
ttk.TTkButton(parent=root, pos=(13,1), size=(15,3), border=True, text="Button4")
root.mainloop()
The above code produces the following output:
βββββββββββββββ
β βββββββββββββββ
β Button1 β Button4 β
β βββββββββββββββ
βββββββββββββββ
ββββββββββ
βButton2 βββββββββββ
β ββButton3 β
ββββββββββββββββββββ
2 - Simple TTkVBoxLayoutπΆοΈ
Following is the code to execute VBox Example in pyTermTk (tryItOnline)
import TermTk as ttk
# Set the VBoxLayout as default in the terminal widget
root = ttk.TTk(layout=ttk.TTkVBoxLayout())
# Attach 4 buttons to the root widget
ttk.TTkButton(parent=root, border=True, text="Button1")
ttk.TTkButton(parent=root, border=True, text="Button2")
ttk.TTkButton(parent=root, border=True, text="Button3")
ttk.TTkButton(parent=root, border=True, text="Button4")
root.mainloop()
The above code produces the following output:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β Button1 β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β Button2 β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β Button3 β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β Button4 β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
3 - Simple TTkHBoxLayoutπΆοΈ
Following is the code to execute HBox Example in pyTermTk (tryItOnline)
import TermTk as ttk
# Set the HBoxLayout as default in the terminal widget
root = ttk.TTk()
root.setLayout(ttk.TTkHBoxLayout())
# Attach 4 buttons to the root widget
ttk.TTkButton(parent=root, border=True, text="Button1")
ttk.TTkButton(parent=root, border=True, text="Button2")
ttk.TTkButton(parent=root, border=True, text="Button3")
ttk.TTkButton(parent=root, border=True, text="Button4")
root.mainloop()
The above code produces the following output
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β Button1 ββ Button2 ββ Button3 ββ Button4 β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
β ββ ββ ββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
4 - Simple TTkGridLayoutπΆοΈ
Following is the code to execute Grid Example in pyTermTk (tryItOnline)
import TermTk as ttk
# Set the GridLayout as default in the terminal widget
gridLayout = ttk.TTkGridLayout(columnMinHeight=0,columnMinWidth=2)
root = ttk.TTk(layout=gridLayout)
# Attach 2 buttons to the root widget using the default method
# this will append them to the first row
ttk.TTkButton(parent=root, border=True, text="Button1")
ttk.TTkButton(parent=root, border=True, text="Button2")
# Attach 2 buttons to a specific position in the grid
gridLayout.addWidget(ttk.TTkButton(parent=root, border=True, text="Button3"), 1,2)
gridLayout.addWidget(ttk.TTkButton(parent=root, border=True, text="Button4"), 3,4)
root.mainloop()
The above code produces the following output
ββββββββββββββββββββββββββ
β ββ β
β Button1 ββ Button2 β
β ββ β
ββββββββββββββββββββββββββ
βββββββββββββ
β β
β Button3 β
β β
βββββββββββββ
βββββββββββββ
β β
β Button4 β
β β
βββββββββββββ
5 - Nested LayoutsπΆοΈ
Following is the code to execute Nested Layouts Example in pyTermTk (tryItOnline)
import TermTk as ttk
# Set the GridLayout as default in the terminal widget
root = ttk.TTk()
gridLayout = ttk.TTkGridLayout()
root.setLayout(gridLayout)
# Attach 2 buttons to the root widget using the default method
# this will append them to the first row
# NOTE: it is not recommended to use this legacy method in a gridLayout
ttk.TTkButton(parent=root, border=True, text="Button1")
ttk.TTkButton(parent=root, border=True, text="Button2")
# Attach 2 buttons to a specific position in the grid
gridLayout.addWidget(ttk.TTkButton(border=True, text="Button3"), 1,2)
gridLayout.addWidget(ttk.TTkButton(border=True, text="Button4"), 2,4)
# Create a VBoxLayout and add it to the gridLayout
vboxLayout = ttk.TTkVBoxLayout()
gridLayout.addItem(vboxLayout,1,3)
# Attach 2 buttons to the vBoxLayout
vboxLayout.addWidget(ttk.TTkButton(border=True, text="Button5"))
vboxLayout.addWidget(ttk.TTkButton(border=True, text="Button6"))
root.mainloop()
The above code produces the following output
ββββββββββββββββββββββ
β ββ β
β Button1 ββ Button2 β
β ββ β
ββββββββββββββββββββββ
ββββββββββββββββββββββ
β ββ Button5 β
β Button3 ββββββββββββ
β ββββββββββββ
β ββ Button6 β
ββββββββββββββββββββββ
βββββββββββ
β β
β Button4 β
β β
βββββββββββ
6 - Rowspan/Colspan in Grid LayoutπΆοΈ
Following is the code to execute row/colspan Example in pyTermTk (tryItOnline)
import TermTk as ttk
root = ttk.TTk()
gridLayout = ttk.TTkGridLayout()
root.setLayout(gridLayout)
# | x = 0 | x = 1 | x = 2 |
# | | | |
# βββββββββββββββββββββββββββββ ββββββ
# βy=0 x=0 h=1 w=2 ββy=0 x=2 β y = 0
# β Button1 ββh=2 w=1 β
# βββββββββββββββββββ β ββββββ
# βββββββββββ β Button2 β y = 1
# βy=1 x=0 β βββββββββββ
# βh=2 w=1 βββββββββββββββββββ ββββββ
# β ββy=2 x=1 h=1 w=2 | y = 2
# β Button3 ββ Button4 β
# βββββββββββββββββββββββββββββ ββββββ
gridLayout.addWidget(ttk.TTkButton(border=True, text="Button1"), 0,0, 1,2)
gridLayout.addWidget(ttk.TTkButton(border=True, text="Button2"), 0,2, 2,1)
gridLayout.addWidget(ttk.TTkButton(border=True, text="Button3"), 1,0, 2,1)
# It is possible to expand the names
gridLayout.addWidget(ttk.TTkButton(border=True, text="Button4"), row=2, col=1, rowspan=1, colspan=2)
root.mainloop()
The above code produces the following output
ββββββββββββββββββββββββββββββββββββββ
β ββ β
β Button1 ββ β
β ββ β
ββββββββββββββββββββββββββ Button2 β
βββββββββββββ β β
β β β β
β β β β
β β βββββββββββββ
β Button3 ββββββββββββββββββββββββββ
β ββ β
β ββ Button4 β
β ββ β
ββββββββββββββββββββββββββββββββββββββ