To create a Python app with a GUI, use PySide to add widgets and connect their signals (“clicked”, etc.) to slots (Python functions).
Here is the complete code (65 lines) of a simple app with two buttons to add and erase bonds. The app first creates two buttons buttonAddBonds
and buttonEraseBonds
, and places them in a vertical layout. Then, these two buttons are connected to two Python slots: onAddBonds
and onEraseBonds
. The slots use the samson package to interact with the active document and the molecules it contains.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
from PySide6.QtWidgets import (QPushButton, QVBoxLayout, QDialog) from PySide6.QtCore import (Qt, Slot) import os 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() |
Paste this code in SAMSON’s Code Editor, and just click run to start the App !