Embedded app: a simple GUI with buttons#

This example demonstrates how to create a simple embedded app in SAMSON that adds and removes bonds.

Note

SAMSON uses Qt6 for GUI and the integrated Python environment installs PySide6 (Qt6 bindings for Python) for you. So, to create GUI, please use PySide6 or the qtpy wrapper which will automatically select the available Qt Python package.

Note

To learn more how to create GUIs with Qt, please refer to the PySide6 documentation.

You can also download it as a SAMSON document: https://www.samson-connect.net/app/main?key=document&uuid=994515df-8015-4d44-baa6-1321249389bc

Example: An embedded app that adds and removes bonds#
from qtpy.QtWidgets import QPushButton, QVBoxLayout, QDialog
from qtpy.QtCore import Qt, Slot

class App(QDialog):

    def __init__(self, parent=None):
    
        super(App, self).__init__(parent)
    
        # build interface
        
        layout = QVBoxLayout()

        buttonAddBonds = QPushButton("Add bonds")
        layout.addWidget(buttonAddBonds)

        buttonEraseBonds = QPushButton("Erase bonds")
        layout.addWidget(buttonEraseBonds)

        self.setLayout(layout)
        self.setMinimumWidth(250)
        
        # connect button signals to slots

        buttonAddBonds.clicked.connect(self.onAddBonds)
        buttonEraseBonds.clicked.connect(self.onEraseBonds)

    @Slot()
    def onAddBonds(self):

        # find all structural models

        structuralModelIndexer = SAMSON.getNodes("node.type structuralModel")
        
        # create bonds

        SAMSON.beginHolding("Add bonds") # make it undoable

        for structuralModel in structuralModelIndexer:       
            structuralModel.createCovalentBonds()

        SAMSON.endHolding()

    @Slot()
    def onEraseBonds(self):

        # find all bonds

        bondIndexer = SAMSON.getNodes("node.type bond")

        # erase bonds

        SAMSON.beginHolding("Erase bonds") # make it undoable
        
        for bond in bondIndexer:
            bond.erase()

        SAMSON.endHolding()

if __name__ == '__main__':

    app = App()
    app.show()
The app interface