UI

This example covers how to create some nice user interfaces.

PySide

This is the recommended way to create your UI in Guerilla.

Let’s build a simple app

import os, sys
from guerilla import Document
from PySide import QtGui

class Dummy(QtGui.QWidget):
    def __init__(self):
        super(Dummy, self).__init__()
        self.setWindowTitle('Pyside guerilla')

        # 2 push buttons...
        self.newButton = QtGui.QPushButton('New Scene')
        self.saveButton = QtGui.QPushButton('Save Scene')

        # ...in a vertical layout
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.newButton)
        vbox.addWidget(self.saveButton)

        # set our vertical layout
        self.setLayout(vbox)

        # connect signals
        self.newButton.clicked.connect(self.newScene)
        self.saveButton.clicked.connect(self.saveScene)

    def newScene(self):
        d = Document()
        d.new()

    def saveScene(self):
        d = Document()
        if d.getfilename() is None:
            homeFolder = os.path.expanduser('~')
            res = QtGui.QFileDialog.getSaveFileName(self, 'Save Guerilla project', homeFolder, "Guerilla project (*.gproject)")

            if str(res[0]):
                print 'saving %s' % str(res[0])
                d.save(str(res[0]))
        else:
            # save over current file
            d.save()

app = QtGui.QApplication.instance()
if app is None:
    app = QtGui.QApplication(sys.argv)
d1 = Dummy()
d1.show()

Pyside Advanced 1

Let’s use the event class to start our dummy window right after a new document is created

def foo(*args):
    from PySide import QtGui
    app = QtGui.QApplication.instance()
    # avoid garbage collection
    globals()['qtDummyWindow'] = Dummy()
    globals()['qtDummyWindow'].show()

event.register('newdocument', foo)

Pyside Advanced 2

Let’s override the guerilla default color picker

def qcolorpicker(luaObj, lx, ly):

    import sys
    import lua
    from PySide import QtGui
    from guerilla import fromLua

    # get python obj
    win = fromLua(luaObj)
    # parent window
    pwin = win.getparent(None)

    # get input (ie. color plug)
    inputs = pwin.Input.getdependencies()
    ic = inputs[0].get()
    qic = QtGui.QColor.fromRgbF(ic[0], ic[1], ic[2])

    # look for an existing qt app otherwise create one
    app = QtGui.QApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)

    # show dialog
    res = QtGui.QColorDialog.getColor(initial=qic)
    if res.isValid():
        for i in inputs:
            i.set([res.redF(), res.greenF(), res.blueF()])

lua.globals().LUIColorBoxBoxVT.onLDoubleClick = qcolorpicker

Going further

  • Try the next example in Examples
  • Explore the API documentation