Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Populate TouchBar

Maxmad68 edited this page Jun 19, 2021 · 8 revisions

To add items to the TouchBar, just use function PyTouchBar.set_touchbar([item1, item2, ...])
Items are all defined in PyTouchBar.TouchBarItems.
There are many items you can add, such as:

Example showing two labels:

lbl1 = PyTouchBar.TouchBarItems.Label(text='Hello')
space = TouchBarSpace.Flexible()
lbl2 = PyTouchBar.TouchBarItems.Label(text='World')
ptb.set_touchbar([lbl1, space, lbl2])

Items

Spaces

Spaces are the most common items in a TouchBar.
In fact, you cannot set the position (left, right, center,...) of an item in a TouchBar. To place your items, you need to use spaces between them. Flexible spaces will take all the space they can by pushing other items to the sides of the TouchBar.
Example: Settings TouchBar like this: [item1, Flexible Space, item2] will show the item1 on the left of the TouchBar and item2 on the right of the TouchBar.

There are three different space items:

  • Small space: item = PyTouchBar.TouchBarItems.Space.Small()
  • Large space: item = PyTouchBar.TouchBarItems.Space.Large()
  • Flexible space: item = PyTouchBar.TouchBarItems.Space.Flexible()
    There is no parameters to set on space items.

Label

A label is an item showing text.

label = PyTouchBar.TouchBarItems.Label(text = 'Foo Bar')

Example

Parameters:
  • text (String, get & set) : The text to be shown on the label
  • text_color (tuple, get & set) : The color of the text to be shown as (r, g, b, a) where values are decimal numbers between 0 and 1. You can use the color constants.
  • alignment (Alignment, get & set) : The alignment of the text in the label. Alignments are defined in constants.
  • font_name (String, get & set) : Default is Arial. The font name of the text to show
  • font_size (Int, get & set) : Default is 16. The font size of the text to show

Button

A button the user can click, that will call actions.

def function(button):
  print ('Button clicked!')

button = PyTouchBar.TouchBarItems.Button(title = 'Click me!', action = function)

Example

Parameters:
  • title (String or None, get & set) : The string that will be displayed in the button
  • color (tuple or None, get & set) : The button background color. Formatted as (r, g, b, a) where values are decimal numbers between 0 and 1. You can use the color constants.
  • image (String or None, get & set) : A path to an image file that will be shown on the button
  • image_position (ImagePosition) : The position of the image relative to the title. Image positions are defined in constants.
  • image_scale (ImageScale) : The image scaling. Image scales are defined in constants.
  • action (function taking button as argument) : The function that will be called when the user touchs the button

ColorPicker

A color picker is a button, which open a color selector when pressed.

def function(picker):
  print ('Color:', picker.color)

cpk = PyTouchBar.TouchBarItems.ColorPicker(action = function, type = PyTouchBar.Items.ColorPicker.Type.color)

Example Example

Parameters:
  • alpha (Bool) : True if user can select alpha value, False otherwise
  • type (ColorPicker.Type): The type of color picker. Color picker types are defined in constants.
  • image (String) : If type is ColorPickerType.image, you can define the image displayed in the color picker button by specifying this parameter. This is the path of an image file.
  • action (function taking ColorPicker as argument) : The function that will be called when the user change the value of the color picker
  • color (tuple, get & set) : You can retrieve and set the selected color by using or setting this variable. Formatted as (r, g, b, a) where values are decimal numbers between 0 and 1. You can use the color constants.

Slider

An item you can drag to choose a numeric value

def function(slider):
  print ('Value:', slider.value * 100)

slider = PyTouchBar.TouchBarItems.Slider(action = function)

Example

Parameters:
  • title (String) : The title of the slider, that will be displayed next to the slider
  • value (Float, get & set) : The default value as a decimal number between 0 and 1
  • color (tuple) : The tint color of the slider. Formatted as (r, g, b, a) where values are decimal numbers between 0 and 1. You can use the color constants.
  • action (function taking slider as argument) : The function that will be called when the user change the value of the slider

Stepper

An item that allows you to select a numeric value (macOS 10.15 +)

def function(stepper):
  print ('Value:', stepper.value)

slider = PyTouchBar.TouchBarItems.Stepper(min = 0, max = 100, action = function)

Example

Parameters:
  • min (Float, get & set) : The minimum value of the stepper
  • max (Float, get & set) : The maximum value of the stepper
  • step (Float, get & set) : Increment value
  • value (Float, get & set) : Current selected value
  • action (function taking slider as argument) : The function that will be called when the user change the value of the slider

SegmentedControls

Segmented Controls are groups of buttons, which can act like normal buttons, or to select one of them, or select several of them.
There are two ways to define Segmented Controls depending on the complexity of its items.

Easy way: With strings
This is the best way to define the SegmentedControls if its controls are basic, if they just contains text.

sc = PyTouchBar.TouchBarItems.SegmentedControls(controls = ["Hello","World","Foobar!"])

Example

Complex way: With a list of Controls
If the controls you want to show are more complexe than just showing text, (like having images, defined width,...), then you should create a SegmentedControls.Control instance.
Note: You can define the SegmentedControls with both Control instances and string

control1 = PyTouchBar.TouchBarItems.SegmentedControls.Control(title = "Hello")
control2 = PyTouchBar.TouchBarItems.SegmentedControls.Control(title = "World", image = "~/MyImage.png")
sc = PyTouchBar.TouchBarItems.SegmentedControls(controls = [control1, control2, "FooBar"])

Example

Parameters:
  • controls ([String and/or SegmentedControls.Control) : Controls of the SegmentedControls
  • type (SegmentedControls.Type): The type of segmented controls (select one control at a time, select multiple controls at a time, of just buttons)
  • action (function taking SC as argument) : The function that will be called when the user press/select a control
Methods:
  • sc.selectedItems() -> [SegmentedControls.Control] : When called, will return the list containing every selected controls

Controls

control1 = PyTouchBar.TouchBarItems.SegmentedControls.Control(title = "Hello")
Parameters:
  • title (String or None, get & set) : The string that will be displayed in the button
  • image (String or None) : A path to an image file that will be shown on the button
  • width (Int or None) : The width of the control (in px)
  • enabled (Bool) : Whether the control is selectable of not. If False, the button is gray.
  • selected (Bool, get & set) : The state of the control selection.
  • image_scale (ImageScale) : The image scaling. Image scales are defined in constants.

Popover

A button that will show another TouchBar when clicked.

label = TouchBarLabel(text = 'Inside my Popover')    

popover = PyTouchBar.TouchBarItems.Popover([label])

Example Example

Parameters:
  • title (String) : The title of the popover, that will be displayed next on the button
  • shows_close_button (Bool) : If True, once the popover is opened, will show a close button. If False, it won't and the only way to close the popover is to call popover.close()
  • holdItems (list) : A list containing items that will be shown if the button is touched and hold by the user. It's kind of another TouchBar
Methods:
  • popover.reload() : If the sub-TouchBar content is modified and the sub-TouchBar needs to be reloaded, this command will do so.
  • popover.open() : Open the popover
  • popover.close() : Close the popover