It looks like you're new here. If you want to get involved, click one of these buttons!
I am attempting to generate output information in a tabbed layout using PyQT5. In the case that I have multiple devices, I would expect multiple tabs to show up in the output with information corresponding to their respective device.
For some reason, I am getting a single tab, and a single button. Basically like the information is getting written over.
The issue is that I do not know how to dynamically add new tabs and hold on to all of the information inside them properly upon each iteration. I will not know the number of devices or device parameters ahead of time, so I can not set this information in code. It must be dynamic.
Here is my current code:
` # $description: Testing
# $show-in-menu
import pya
import re, sys
import os
# Create tab widget within main layout
self.tab_widget = pya.QTabWidget()
self.scroll_layout.addWidget( self.tab_widget )
tab = []
tab_layout = []
data_for_tabs = dict()
data_for_tabs["Device1"] = dict()
data_for_tabs["Device1"]["Parameter1"] = dict()
data_for_tabs["Device1"]["Parameter1"]["x"] = 1.2
data_for_tabs["Device1"]["Parameter1"]["y"] = 1.5
data_for_tabs["Device1"]["Parameter2"] = dict()
data_for_tabs["Device1"]["Parameter2"]["x"] = 1.4
data_for_tabs["Device1"]["Parameter2"]["y"] = 1.3
data_for_tabs["Device2"] = dict()
data_for_tabs["Device2"]["Parameter1"] = dict()
data_for_tabs["Device2"]["Parameter1"]["x"] = 2.2
data_for_tabs["Device2"]["Parameter1"]["y"] = 2.5
data_for_tabs["Device2"]["Parameter2"] = dict()
data_for_tabs["Device2"]["Parameter2"]["x"] = 2.4
data_for_tabs["Device2"]["Parameter2"]["y"] = 2.3
# First generate tabs
tabNumber = 0
for key in data_for_tabs.keys():
# Create tab
tab.append( pya.QWidget() )
tab_layout.append( pya.QVBoxLayout() )
tab[tabNumber].setLayout( tab_layout[tabNumber] )
self.tab_widget.addTab( tab[tabNumber], key )
self.tab = tab[tabNumber]
self.tab_layout = tab_layout[tabNumber]
self.runExtraction( data_for_tabs[key], self.tab, self.tab_layout )
tabNumber += 1
def runExtraction( self, data, tab, tab_layout ):
row = 0
for key, value in data.items():
outGroupBox = pya.QGroupBox( key )
self.out_layout = pya.QHBoxLayout()
for pkey, pval in value.items():
txtlbl = pya.QLabel( tab )
txtlbl.setText(f"{pkey} = ")
txt = pya.QLineEdit( tab )
txt.setText(f"{pval}")
txt.setReadOnly( True )
self.out_layout.addWidget( txtlbl )
self.out_layout.addWidget( txt )
self.button = pya.QPushButton()
self.button.text = f"{key}"
self.out_layout.addWidget( self.button )
# Reformat
outGroupBox.setLayout( self.out_layout )
outGroupBox.update()
setattr( self, "outGroupBox" + str(row), outGroupBox )
tab_layout.addWidget( getattr( self, "outGroupBox" + str(row) ) )
row += 1
# Add stretch so everything sits at the top
tab_layout.addStretch()
`
Please feel free to leave comments or provide any help. My guess is that I will need to create some sort of class for the tabbed layout, but I am not sure.
Comments
Hi ManzAutomations,
here's an example of tab that holds random amound of data dynamically
The concept is to create a page widget that can be used repeatly, so later we can feed parameters into page and add to tab widget using loops.
@ManzAutomations Your code does not execute, so I can't debug the problem.
In general you need to properly set the widget's parents in order to establish the Qt ownership. This is prevent Python from destroying the widgets once the variables go out of scope. @RawrRanger's code does the right thing by using the correct parent from the tab widget ("parent" argument).
Here is another basic code to generate two tabs. Please note the comments:
Matthias
@Matthias , I tried to give the full code, but it would not let me given there were too many characters. I will try again...
'
# $description: Testing
# $show-in-menu
'
I have tried using setattr and getattr to attach numbers to the tabs, but for some reason, that is causing me some issues with the tabs even showing up on the application.
I am also curious if the issue is my version? Maybe if the version of KLayout/PyQT5 is too old, that may be the issue. At this point, I have no idea.
Hi ManzAutomations
I slightly change the UI part of you code into a widget to hold dynamyc data.

The example is as below, the parameter input part is generated randomly to simulate a dynamic data set.
Hi ManzAutomations
As to why the tabs is not showing correctly, this is due to the widget or tab is not set to bind with a parent widget.
System will automatically remove the unbinded widget to free up memory and results in tabs missing.
for instance, we would like to add TabPageWidget to UI with binding, the way to do this is:
Method-1
Method-2
Class declaration
The parent parameter will be binded using "init(parent)" , make sure your custom widget did pass this parameter to it's "super" class.
@RawrRanger That fixed my issue! Thank you so much!