Periodic table widgets
This example uses the widgets with the standard builtin elements list.
from silx.gui import qt
from silx.gui.widgets.PeriodicTable import PeriodicTable, PeriodicCombo, PeriodicList
a = qt.QApplication([])
w = qt.QTabWidget()
ptable = PeriodicTable(w, selectable=True)
pcombo = PeriodicCombo(w)
plist = PeriodicList(w)
w.addTab(ptable, "PeriodicTable")
w.addTab(plist, "PeriodicList")
w.addTab(pcombo, "PeriodicCombo")
ptable.setSelection(['H', 'Fe', 'Si'])
plist.setSelectedElements(['H', 'Be', 'F'])
pcombo.setSelection("Li")
def change_list(items):
print("New list selection:", [item.symbol for item in items])
def change_combo(item):
print("New combo selection:", item.symbol)
def click_table(item):
print("New table click:", item.symbol)
def change_table(items):
print("New table selection:", [item.symbol for item in items])
ptable.sigElementClicked.connect(click_table)
ptable.sigSelectionChanged.connect(change_table)
plist.sigSelectionChanged.connect(change_list)
pcombo.sigSelectionChanged.connect(change_combo)
w.show()
a.exec_()
The second example explains how to define custom elements.
from silx.gui import qt
from silx.gui.widgets.PeriodicTable import PeriodicTable, PeriodicCombo, PeriodicList
from silx.gui.widgets.PeriodicTable import PeriodicTableItem
# subclass PeriodicTableItem
class MyPeriodicTableItem(PeriodicTableItem):
"New item with added mass number and number of protons"
def __init__(self, symbol, Z, A, col, row, name, mass,
subcategory=""):
PeriodicTableItem.__init__(
self, symbol, Z, col, row, name, mass,
subcategory)
self.A = A
"Mass number (neutrons + protons)"
self.num_neutrons = A - Z
"Number of neutrons"
# build your list of elements
my_elements = [MyPeriodicTableItem("H", 1, 1, 1, 1, "hydrogen",
1.00800, "diatomic nonmetal"),
MyPeriodicTableItem("He", 2, 4, 18, 1, "helium",
4.0030, "noble gas"),
# etc ...
]
app = qt.QApplication([])
ptable = PeriodicTable(elements=my_elements, selectable=True)
ptable.show()
def click_table(item):
"Callback function printing the mass number of clicked element"
print("New table click, mass number:", item.A)
ptable.sigElementClicked.connect(click_table)
app.exec_()
Bases: PyQt4.QtGui.QWidget
Periodic Table widget
The following example shows how to connect clicking to selection:
from silx.gui import qt
from silx.gui.widgets.PeriodicTable import PeriodicTable
app = qt.QApplication([])
pt = PeriodicTable()
pt.sigElementClicked.connect(pt.elementToggle)
pt.show()
app.exec_()
To print all selected elements each time a new element is selected:
def my_slot(item):
pt.elementToggle(item)
selected_elements = pt.getSelection()
for e in selected_elements:
print(e.symbol)
pt.sigElementClicked.connect(my_slot)
When any element is clicked in the table, the widget emits this signal and sends a PeriodicTableItem object.
When any element is selected/unselected in the table, the widget emits this signal and sends a list of PeriodicTableItem objects.
Note
To enable selection of elements, you must set selectable=True when you instantiate the widget. Alternatively, you can also connect sigElementClicked to elementToggle() manually:
pt = PeriodicTable()
pt.sigElementClicked.connect(pt.elementToggle)
Parameters: |
|
---|
Return a list of selected elements, as a list of PeriodicTableItem objects.
Returns: | Selected items |
---|---|
Return type: | list(PeriodicTableItem) |
Set selected elements.
This causes the sigSelectionChanged signal to be emitted, even if the selection didn’t actually change.
Parameters: | symbols (list(str)) – List of symbols of elements to be selected (e.g. [“Fe”, “Hg”, “Li”]) |
---|
Bases: PyQt4.QtGui.QTreeWidget
List of atomic elements in a QTreeView
Parameters: |
|
---|
When any element is selected/unselected in the widget, it emits this signal and sends a list of currently selected PeriodicTableItem objects.
Get a list of selected elements, as a list of PeriodicTableItem objects.
Returns: | Selected elements |
---|---|
Return type: | list(PeriodicTableItem) |
Bases: PyQt4.QtGui.QComboBox
Combo list with all atomic elements of the periodic table
Parameters: |
|
---|
Signal emitted when the selection changes. Send PeriodicTableItem object representing selected element
Periodic table item, used as generic item in PeriodicTable, PeriodicCombo and PeriodicList.
This implementation stores the minimal amount of information needed by the widgets:
- atomic symbol
- atomic number
- element name
- atomic mass
- column of element in periodic table
- row of element in periodic table
You can subclass this class to add additional information.
Parameters: |
|
---|
Atomic symbol (e.g. H, He, Li...)
Atomic number (Proton number)
1-based column index of element in periodic table
1-based row index of element in periodic table
PeriodicTableItem name (“hydrogen”, ...)
Atomic mass (gram per mol)
Subcategory, based on physical properties (e.g. “alkali metal”, “noble gas”...)
Bases: silx.gui.widgets.PeriodicTable.PeriodicTableItem
PeriodicTableItem with an added bgcolor. The background color can be passed as a parameter to the constructor. If it is not specified, it will be defined based on subcategory.
Parameters: | bgcolor (str) – Custom background color for element in periodic table, as a RGB string #RRGGBB |
---|
Dictionary defining RGB colors for each subcategory.
Background color of element in the periodic table, based on its subcategory. This should be a string of a hexadecimal RGB code, with the format #RRGGBB. If the subcategory is unknown, use white (#FFFFFF)