Window

class vanilla.Window(*args, **kwargs)

A window capable of containing controls.

../_images/Window.png

To add a control to a window, simply set it as an attribute of the window.

from vanilla import Window, Button, TextBox

class WindowDemo:

    def __init__(self):
        self.w = Window((200, 70), "Window Demo")
        self.w.myButton = Button((10, 10, -10, 20), "My Button")
        self.w.myTextBox = TextBox((10, 40, -10, 17), "My Text Box")
        self.w.open()

WindowDemo()

No special naming is required for the attributes. However, each attribute must have a unique name.

posSize Tuple of form (left, top, width, height) representing the position and size of the window. It may also be a tuple of form (width, height). In this case, the window will be positioned on screen automatically.

title The title to be set in the title bar of the window.

minSize Tuple of the form (width, height) representing the minimum size that the window can be resized to.

maxSize Tuple of the form (width, height) representing the maximum size that the window can be resized to.

textured Boolean value representing if the window should have a textured appearance or not.

autosaveName A string representing a unique name for the window. If given, this name will be used to store the window position and size in the application preferences.

closable Boolean value representing if the window should have a close button in the title bar.

miniaturizable Boolean value representing if the window should have a minimize button in the title bar.

initiallyVisible Boolean value representing if the window will be initially visible. Default is True. If False, you can show the window later by calling window.show().

fullScreenMode An indication of the full screen mode. These are the options:

None

The window does not allow full screen.

“primary”

Corresponds to NSWindowCollectionBehaviorFullScreenPrimary.

“auxiliary”

Corresponds to NSWindowCollectionBehaviorFullScreenAuxiliary.

titleVisible Boolean value indicating if the window title should be displayed.

fullSizeContentView Boolean value indicating if the content view should be the full size of the window, including the area underneath the titlebar and toolbar.

screen A NSScreen object indicating the screen that the window should be drawn to. When None the window will be drawn to the main screen.

addAutoPosSizeRules(**kwargs)

Add auto layout rules for controls/view in this view.

rules must by a list of rule definitions. Rule definitions may take two forms:

key

value

“view1”

The vanilla wrapped view for the left side of the rule.

“attribute1”

The attribute of the view for the left side of the rule. See below for options.

“relation” (optional)

The relationship between the left side of the rule and the right side of the rule. See below for options. The default value is “==”.

“view2”

The vanilla wrapped view for the right side of the rule.

“attribute2”

The attribute of the view for the right side of the rule. See below for options.

“multiplier” (optional)

The constant multiplied with the attribute on the right side of the rule as part of getting the modified attribute. The default value is 1.

“constant” (optional)

The constant added to the multiplied attribute value on the right side of the rule to yield the final modified attribute. The default value is 0.

The attribute1 and attribute2 options are:

value

AppKit equivalent

“left”

NSLayoutAttributeLeft

“right”

NSLayoutAttributeRight

“top”

NSLayoutAttributeTop

“bottom”

NSLayoutAttributeBottom

“leading”

NSLayoutAttributeLeading

“trailing”

NSLayoutAttributeTrailing

“width”

NSLayoutAttributeWidth

“height”

NSLayoutAttributeHeight

“centerX”

NSLayoutAttributeCenterX

“centerY”

NSLayoutAttributeCenterY

“baseline”

NSLayoutAttributeBaseline

“lastBaseline”

NSLayoutAttributeLastBaseline

“firstBaseline”

NSLayoutAttributeFirstBaseline

Refer to the NSLayoutAttribute documentation for the information about what each of these do.

The relation options are:

value

AppKit equivalent

“<=”

NSLayoutRelationLessThanOrEqual

“==”

NSLayoutRelationEqual

“>=”

NSLayoutRelationGreaterThanOrEqual

Refer to the NSLayoutRelation documentation for the information about what each of these do.

metrics may be either None or a dict containing key value pairs representing metrics keywords used in the rules defined with strings.

addToolbar(**kwargs)

Add a toolbar to the window.

toolbarIdentifier A string representing a unique name for the toolbar.

toolbarItems An ordered list of dictionaries containing the following items:

itemIdentifier

A unique string identifier for the item. This is only used internally.

label (optional)

The text label for the item. Defaults to None.

paletteLabel (optional)

The text label shown in the customization palette. Defaults to label.

toolTip (optional)

The tool tip for the item. Defaults to label.

imagePath (optional)

A file path to an image. Defaults to None.

imageNamed (optional)

The name of an image already loaded as a NSImage by the application. Defaults to None.

imageObject (optional)

A NSImage object. Defaults to None.

imageTemplate (optional)

A boolean representing if the image should converted to a template image.

selectable (optional)

A boolean representing if the item is selectable or not. The default value is _False_. For more information on selectable toolbar items, refer to Apple’s documentation.

view (optional)

A `NSView`_ object to be used instead of an image. Defaults to None.

menuRepresentation (optional)

A dict or NSMenuItem to represent the toolbar item when hidden.

visibleByDefault (optional)

If the item should be visible by default pass True to this argument. If the item should be added to the toolbar only through the customization palette, use a value of _False_. Defaults to _True_.

addStandardItems A boolean, specifying whether the standard Cocoa toolbar items should be added. Defaults to True. If you set it to False, you must specify any standard items manually in toolbarItems, by using the constants from the AppKit module:

NSToolbarSeparatorItemIdentifier

The Separator item.

NSToolbarSpaceItemIdentifier

The Space item.

NSToolbarFlexibleSpaceItemIdentifier

The Flexible Space item.

NSToolbarShowColorsItemIdentifier

The Colors item. Shows the color panel.

NSToolbarShowFontsItemIdentifier

The Fonts item. Shows the font panel.

NSToolbarCustomizeToolbarItemIdentifier

The Customize item. Shows the customization palette.

NSToolbarPrintItemIdentifier

The Print item. Refer to Apple’s NSToolbarItem documentation for more information.

displayMode A string representing the desired display mode for the toolbar.

“default”

“iconLabel”

“icon”

“label”

sizeStyle A string representing the desired size for the toolbar

“default”

“regular”

“small”

toolbarStyle A string represetnting the desired toolbar style

“default”

“expanded”

“preference”

“unified”

“unifiedCompact”

Returns a dictionary containing the created toolbar items, mapped by itemIdentifier.

addToolbarItem(**kwargs)

Add a toolbar item to the windows toolbar.

itemData item description with the same format as a toolbarItem description in addToolbar

index An integer, specifying the place to insert the toolbar itemIdentifier.

assignToDocument(**kwargs)

Add this window to the list of windows associated with a document.

document should be a NSDocument instance.

bind(**kwargs)

Bind a callback to an event.

event A string representing the desired event. The options are:

“should close”

Called when the user attempts to close the window. This must return a bool indicating if the window should be closed or not.

“close”

Called immediately before the window closes.

“move”

Called immediately after the window is moved.

“resize”

Called immediately after the window is resized.

“became main”

Called immediately after the window has become the main window.

“resigned main”

Called immediately after the window has lost its main window status.

“became key”

Called immediately after the window has become the key window.

“resigned key”

Called immediately after the window has lost its key window status.

For more information about main and key windows, refer to the Cocoa `documentation`_ on the subject.

callback The callback that will be called when the event occurs. It should accept a sender argument which will be the Window that called the callback.

from vanilla import Window

class WindowBindDemo:

    def __init__(self):
        self.w = Window((200, 200))
        self.w.bind("move", self.windowMoved)
        self.w.open()

    def windowMoved(self, sender):
        print("window moved!", sender)

WindowBindDemo()
center()

Center the window within the screen.

close()

Close the window.

Once a window has been closed it can not be re-opened.

getNSWindow()

Return the NSWindow that this Vanilla object wraps.

getNSWindowController()

Return an NSWindowController for the NSWindow that this Vanilla object wraps, creating a one if needed.

getPosSize()

A tuple of form (left, top, width, height) representing the window’s position and size.

getTitle()

The title in the window’s title bar.

hide()

Hide the window.

isVisible()

A boolean value representing if the window is visible or not.

makeKey()

Make the window the key window.

makeMain()

Make the window the main window.

move(**kwargs)

Move the window by x units and y units.

open()

Open the window.

removeToolbarItem(**kwargs)

Remove a toolbar item by his identifier.

itemIdentifier A unique string identifier for the removed item.

resize(**kwargs)

Change the size of the window to width and height.

select()

Select the window if it is not the currently selected window.

setDefaultButton(**kwargs)

Set the default button in the window.

button will be bound to the Return and Enter keys.

setPosSize(**kwargs)

Set the position and size of the window.

posSize A tuple of form (left, top, width, height).

setTitle(**kwargs)

Set the title in the window’s title bar.

title should be a string.

setToolbarStyle(**kwargs)

Set a toolbar style for the window.

toolbarStyle A string represetnting the desired toolbar style

“default”

“expanded”

“preference”

“unified”

“unifiedCompact”

show()

Show the window if it is hidden.

unbind(**kwargs)

Unbind a callback from an event.

event A string representing the desired event. Refer to bind for the options.

callback The callback that has been bound to the event.