I'm pretty new with Qt in Python, and I need to know how could I get from a file dialog one directory path and show it in a text box inside my ui.
class ControllerLibraryUI(QtWidgets.QDialog):
def __init__(self):
super(ControllerLibraryUI, self).__init__()
self.setWindowTitle('Controller Window')
self.buildUI()
def buildUI(self):
layout = QtWidgets.QVBoxLayout(self)
textWidget = QtWidgets.QWidget()
textLayout = QtWidgets.QHBoxLayout(textWidget)
layout.addWidget(textWidget)
self.saveNameField = QtWidgets.QTextEdit()
textLayout.addWidget(self.saveNameField)
btnWidget = QtWidgets.QWidget()
btnLayout = QtWidgets.QHBoxLayout(btnWidget)
layout.addWidget(btnWidget)
importBtn = QtWidgets.QPushButton('Import')
importBtn.clicked.connect(self.load)
btnLayout.addWidget(importBtn)
closeBtn = QtWidgets.QPushButton('Close')
closeBtn.clicked.connect(self.close)
btnLayout.addWidget(closeBtn)
def load(self ):
filename = QtWidgets.QFileDialog.getOpenFileName()[0]
print filename
def showUI():
try:
ui.close()
except:
ui = ControllerLibraryUI()
ui.show()
return ui
ui = showUI()
This is the basic concept.
for the file dialog :
directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select directory')
and then you assign the directory variable to the setText method from your text box
Related
I'm trying to access a widget that is added to the layout in which it is displayed from another file.
ui_main.py
from ui_main_pages import Ui_MainPages
class UI_MainWindow(object):
def setup_ui(self, parent):
self.central_widget = QWidget()
self.central_widget_layout = QVBoxLayout(self.central_widget)
self.window = CustomWindow()
self.central_widget_layout.addWidget(self.window)
# Add Left, Right, Title frames code ...
self.content_area_frame = QFrame()
self.content_area_layout = QHBoxLayout(self.content_area_frame)
# Import pages from stacked widget
self.load_pages = Ui_MainPages()
self.load_pages.setupUi(self.content_area_left_frame)
ui_main_pages.py
class Ui_MainPages(object):
def setupUi(self, MainPages):
self.main_pages_layout = QVBoxLayout(MainPages)
self.pages = QStackedWidget(MainPages)
self.page_1 = QWidget()
# add page 1 code
self.page_2 = QWidget()
self.page_2_layout = QVBoxLayout(self.page_2)
self.scroll_area = QScrollArea(self.page_2)
self.contents = QWidget()
self.verticalLayout = QVBoxLayout(self.contents)
self.title_label = QLabel(self.contents)
self.verticalLayout.addWidget(self.title_label)
self.description_label = QLabel(self.contents)
self.verticalLayout.addWidget(self.description_label)
self.row_1_layout = QHBoxLayout()
self.row_1_layout.setObjectName(u"row_1_layout")
self.verticalLayout.addLayout(self.row_1_layout)
self.row_2_layout = QHBoxLayout()
self.row_2_layout.setObjectName(u"row_2_layout")
self.verticalLayout.addLayout(self.row_2_layout)
self.row_3_layout = QHBoxLayout()
self.row_3_layout.setObjectName(u"row_3_layout")
self.verticalLayout.addLayout(self.row_3_layout)
self.row_4_layout = QVBoxLayout()
self.row_4_layout.setObjectName(u"row_4_layout")
self.verticalLayout.addLayout(self.row_4_layout)
self.row_5_layout = QVBoxLayout()
self.row_5_layout.setObjectName(u"row_5_layout")
self.verticalLayout.addLayout(self.row_5_layout)
self.scroll_area.setWidget(self.contents)
self.page_2_layout.addWidget(self.scroll_area)
self.pages.addWidget(self.page_2)
Where i add the table widget is in the page_2 to row_5_layout, lets see in the next code
setup_main_window.py
from ui_main import UI_MainWindow
class SetupMainWindow:
def __init__(self):
super().__init__()
# SETUP MAIN WINDOw
# Load widgets from "gui\uis\main_window\ui_main.py"
# ///////////////////////////////////////////////////////////////
self.ui = UI_MainWindow()
self.ui.setup_ui(self)
def setup_gui(self):
# OBJECTS FOR LOAD PAGES
# PAGE 1 - ADD LOGO TO MAIN PAGE
self.logo_svg = QSvgWidget(Functions.set_svg_image("logo_home.svg"))
self.ui.load_pages.logo_layout.addWidget(self.logo_svg, Qt.AlignCenter, Qt.AlignCenter)
# PAGE 2
# TABLE WIDGETS
self.table_widget = PyTableWidget()
self.ui.load_pages.row_5_layout.addWidget(self.table_widget)
In the above file I add the table widget to the layout in ui_main_pages, and in the next file I try load a html file a make a pandas dataframe, then set a custom model to table_widget
app_file.py
from ui_main import UI_MainWindow
class Files:
def __init__(self):
super().__init__()
# IMPORT WIDGETS
self.ui = UI_MainWindow()
self.ui.setup_ui()
def load(self):
global file_name
# FILE DIALOG
dlg_file = QFileDialog.getOpenFileName(parent=self, caption=self.tr("Select a file"),filter=self.tr('HTML Files (*.html)'),)
# READ FILE
if dlg_file:
self.df_squad = FMi.setting_up_pandas(dlg_file[0])
# FMi is a custom package, which read the file and convert it to a dataframe
# self.df_squad is the dataframe
# Below i try to select the table_widget created before and edit it
self.ui.load_pages.row_5_layout.table_widget.setSelectionBehavior(QTableView.SelectRows)
model = CustomPandasModel(self.df_squad)
self.ui.load_pages.row_5_layout.table_widget.setModel(model)
I have already connected a button to load function in Files class, and it works up to the line of "setSelectionBehavior".
The error is:
AttributeError: 'PySide6.QtWidgets.QVBoxLayout' object has no attribute 'table_widget'
EDIT
The last file main.py from where I run all the project
from ui_main import UI_MainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = UI_MainWindow()
self.ui.setup_ui(self)
# SETUP MAIN WINDOW
# ///////////////////////////////////////////////////////////////
SetupMainWindow.setup_gui(self)
self.show()
def btn_clicked(self):
# GET BT CLICKED
btn = SetupMainWindow.setup_btns(self)
# Here i have the code to change pages like next example
# HOME BTN
if btn.objectName() == "btn_home":
# Select Menu
self.ui.left_menu.select_only_one(btn.objectName())
# Load Page 1
MainFunctions.set_page(self, self.ui.load_pages.page_1)
if __name__ == "__main__":
# APPLICATION
# ///////////////////////////////////////////////////////////////
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("icon.ico"))
window = MainWindow()
# EXEC APP
# ///////////////////////////////////////////////////////////////
sys.exit(app.exec())
Github project link
https://github.com/truev0/FM_Excel_Helper
I am building a GUI in python using PyQt5. Data is imported using on_pushButtonLoad_clicked() and get_file() located in TabWidget.
My goal is to:
transfer this data (importedfile) to FirstTab()
update the plot MRChart(importedfile) using the recently imported data.
I have completed the first goal in transferring the data to FirstTab() whereby the plot is generated and can be shown (for demonstrative purposes) in the browser using fig.show(). Although MRChart(importedfile) is connected to the plotting widget energy() by self.browser, the figure fails to show in the GUI.
Code:
class TabWidget(QDialog):
def __init__(self, data):
super(TabWidget, self).__init__()
self.data = data
self.showMaximized()
#create filter object
FilterLayout = QHBoxLayout()
FilterLayout.addWidget(self.createHeader1a(), 2)#column width
#create tab widget object
tabwidget = QTabWidget()
tabwidget.addTab(FirstTab(self.data), "Tab 1")
vbox = QVBoxLayout()
vbox.addLayout(FilterLayout)
vbox.addWidget(tabwidget)
self.setLayout(vbox)
def createHeader1a(self): # function defining characteristics of each group/grid object
HeaderBox = QGroupBox("Import Data")
inputfilebtn = QPushButton("Import")
inputfilebtn.resize(150, 50)
inputfilebtn.clicked.connect(self.on_pushButtonLoad_clicked)
#importrow1
importrow1layout = QHBoxLayout()
importrow1layout.addWidget(inputfilebtn)
importrow1layout.addStretch()
HeaderLayout = QGridLayout()
HeaderLayout.addLayout(importrow1layout, 0, 1)
HeaderBox.setLayout(HeaderLayout)
HeaderBox.setFlat(True)
return HeaderBox
def getfile(self):
option = QFileDialog.Options()
fname = QFileDialog.getOpenFileName(self, 'Open file',
'c:\\', "CSV files (*.csv)", options=option)
global importedfile
importedfile = pd.read_csv(fname[0])
#QtCore.pyqtSlot()
def on_pushButtonLoad_clicked(self):
self.getfile()
FT=FirstTab(data=importedfile)
FT.MRChart(importedfile)
class FirstTab(QWidget):
def __init__(self, data):
super(FirstTab, self).__init__()
self.data = data
# Grid layout of entire tab
layout = QGridLayout()
layout.addWidget(self.energy(), 3, 0)
layout.setRowStretch(3, 3)
layout.setColumnStretch(0, 1)
self.setLayout(layout)
def MRChart(self, importedfile): # pie
fig = go.Pie(labels=importedfile["Label1"], values=importedfile["Label2"])
layout = go.Layout(autosize=True, legend=dict(orientation="h",xanchor='center', x=0.5))# height = 600, width = 1000,
fig = go.Figure(data=fig, layout=layout)
fig.update_layout(margin=dict(t=0, b=0, l=0, r=0))
fig.show()# only included to prove that figure has been created
self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
def energy(self):
groupBox = QGroupBox("Box Title")
self.browser = QtWebEngineWidgets.QWebEngineView(self)
exportfilebtn = QCheckBox("tickbox1")
middleright = QHBoxLayout()
middleright.addWidget(self.browser)
middleright.addWidget(exportfilebtn)
groupBox.setLayout(middleright)
groupBox.setFlat(True)
return groupBox
if __name__ == "__main__":
app = QApplication(sys.argv)
tabwidget = TabWidget(data=None)
tabwidget.show()
app.exec()
Updated code in line with musicamante's answer
class TabWidget(QDialog):
def __init__(self, data):
super(TabWidget, self).__init__()
self.data = data
self.firstTab = FirstTab(self.data)
#create filter object
FilterLayout = QHBoxLayout()
FilterLayout.addWidget(self.createHeader1a(), 2)#column width
#create tab widget object
tabwidget = QTabWidget()
tabwidget.addTab(self.firstTab "Tab 1")
vbox = QVBoxLayout()
vbox.addLayout(FilterLayout)
vbox.addWidget(tabwidget)
self.setLayout(vbox)
def createHeader1a(self): # function defining characteristics of each group/grid object
HeaderBox = QGroupBox("Import Data")
inputfilebtn = QPushButton("Import")
inputfilebtn.resize(150, 50)
inputfilebtn.clicked.connect(self.on_pushButtonLoad_clicked)
#importrow1
importrow1layout = QHBoxLayout()
importrow1layout.addWidget(inputfilebtn)
importrow1layout.addStretch()
HeaderLayout = QGridLayout()
HeaderLayout.addLayout(importrow1layout, 0, 1)
HeaderBox.setLayout(HeaderLayout)
HeaderBox.setFlat(True)
return HeaderBox
def getfile(self):
option = QFileDialog.Options()
fname = QFileDialog.getOpenFileName(self, 'Open file',
'c:\\', "CSV files (*.csv)", options=option)
return pd.read_csv(fname[0])
#QtCore.pyqtSlot()
def on_pushButtonLoad_clicked(self):
importedfile = self.getfile()
if importedfile is None:
return
self.firstTab.MRChart(importedfile)
class FirstTab(QWidget):
def __init__(self, data):
super(FirstTab, self).__init__()
self.data = data
# Grid layout of entire tab
layout = QGridLayout()
layout.addWidget(self.energy(), 3, 0)
layout.setRowStretch(3, 3)
layout.setColumnStretch(0, 1)
self.setLayout(layout)
def MRChart(self, importedfile): # pie
fig = go.Pie(labels=importedfile["Label1"], values=importedfile["Label2"])
layout = go.Layout(autosize=True, legend=dict(orientation="h",xanchor='center', x=0.5))# height = 600, width = 1000,
fig = go.Figure(data=fig, layout=layout)
fig.update_layout(margin=dict(t=0, b=0, l=0, r=0))
fig.show()# only included to provde figure is created
self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
def energy(self):
groupBox = QGroupBox("Box Title")
self.browser = QtWebEngineWidgets.QWebEngineView(self)
exportfilebtn = QCheckBox("tickbox1")
middleright = QHBoxLayout()
middleright.addWidget(self.browser)
middleright.addWidget(exportfilebtn)
groupBox.setLayout(middleright)
groupBox.setFlat(True)
return groupBox
if __name__ == "__main__":
app = QApplication(sys.argv)
tabwidget = TabWidget(data=None)
tabwidget.show()
app.exec()
You're creating a new instance of FirstTab, instead of using the existing one.
You must keep a reference to the first instance and then call its MRChart function.
Also, you should not use globals: getfile() should return the value, and you should use that returned value in on_pushButtonLoad_clicked.
class TabWidget(QDialog):
def __init__(self, data):
# ...
self.firstTab = FirstTab(self.data)
tabwidget.addTab(self.firstTab, "Tab 1")
# ...
def getfile(self):
option = QFileDialog.Options()
fname, _ = QFileDialog.getOpenFileName(self, 'Open file',
'c:\\', "CSV files (*.csv)", options=option)
if fname:
return pd.read_csv(fname)
#QtCore.pyqtSlot()
def on_pushButtonLoad_clicked(self):
importedfile = self.getfile()
if importedfile is None:
return
self.firstTab.MRChart(importedfile)
Note that it's good practice to show the window only after adding its elements, and it's also usually better to not call show*() in the __init__. It's also pointless to use resize() on a widget that is being added to a layout.
I have already associated my PyQt5 Gui with Excel.
I would like to input '2000' into Excel via PyQt5 Gui program even if I do not input anything on it.
The explanation photo is below:
even if I do not input anything in PyQt5 Gui,
'2000' should be typed in Excel.
However, the point is if anything input in PyQt5 Gui, it should be written in Excel as it was written on PyQt5.
The code that I made is below:
class Ship_Use_Tug_Input_Program(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
visbl = QLabel('Visibility(m)', self)
visbl.move(60, 450)
vis_ent = QLineEdit(self)
vis_ent.move(180, 445)
file = pathlib.Path('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
if file.exists():
pass
else:
file=Workbook()
sheet = file.active
file.save('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
def save_to_excel(self):
file = openpyxl.load_workbook('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
sheet = file.active
sheet.cell(column=10, row=sheet.max_row, value=vis_ent.text())
file.save('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
btn_save = QPushButton('S a v e', self)
btn_save.clicked.connect(save_to_excel)
btn_save.move(400,510)
self.setWindowTitle('Ship use tug input program')
self.setFixedSize(945, 570)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Ship_Use_Tug_Input_Program()
sys.exit(app.exec_())
You should always add widgets as attributes, so you can access them in other methods:
class Ship_Use_Tug_Input_Program(QWidget):
...
def initUI(self):
self.visbl = QLabel('Visibility(m)', self)
self.visbl.move(60, 450)
self.vis_ent = QLineEdit(self)
self.vis_ent.move(180, 445)
Now you can get the input text:
def save_to_excel(self):
...
sheet = file.active
value = self.vis_ent.text().strip() or '2000'
sheet.cell(column=10, row=sheet.max_row, value=value)
...
If vis_ent is empty or whitespace-only, the default value will be used instead.
UPDATE:
Here is what your whole class should look like:
class Ship_Use_Tug_Input_Program(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.visbl = QLabel('Visibility(m)', self)
self.visbl.move(60, 450)
self.vis_ent = QLineEdit(self)
self.vis_ent.move(180, 445)
self.btn_save = QPushButton('S a v e', self)
self.btn_save.clicked.connect(self.save_to_excel)
self.btn_save.move(400, 510)
self.setWindowTitle('Ship use tug input program')
self.setFixedSize(945, 570)
self.show()
file = pathlib.Path('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
if file.exists():
pass
else:
file=Workbook()
sheet = file.active
file.save('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
def save_to_excel(self):
file = openpyxl.load_workbook('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
sheet = file.active
value = self.vis_ent.text().strip() or '2000'
sheet.cell(column=10, row=sheet.max_row, value=value)
file.save('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
I have a problem; How can i get the path (directory) from "QListWidget" selected item.
When I 'am using os.path, this gives me Autodesk Maya Directory. But i need directory of my selected item from QListWidget item.
from PySide.QtGui import *
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
import os
import maya.cmds as cm
Import maya commands from maya as cm. This not include python source files.
class projectManagerMainWindow(MayaQWidgetDockableMixin, QWidget):
def __init__(self):
super(projectManagerMainWindow, self).__init__()
self.selectProject = QComboBox()
self.selectProject.setFixedHeight(30)
items = ["Main Projects","Assets"]
self.selectProject.addItems(items)
self.createNewProject = QPushButton()
self.createNewProject.setFixedSize(100,30)
icon = QPixmap('C:/Users/Faithcure/Documents/maya/2016/scripts/projectManager/new.png')
self.createNewProject.setText("New Project")
self.createNewProject.setIcon(icon)
self.dir_label = QLabel()
def select_driectory():
select_path = cm.fileDialog2(fileMode=3, okc = "Select")
self.sceneList.clear()
for path in select_path:
folders = os.listdir(path)
self.sceneList.addItems(folders)
self.dir_label.setText(path)
self.choose_directory = QPushButton()
self.choose_directory.setText("Directory")
self.choose_directory.setIcon(icon)
self.choose_directory.setFixedSize(100,30)
self.choose_directory.clicked.connect(select_driectory)
def new_project_window():
import newProject
reload(newProject)
ui = newProject.start()
ui.exec_()
def selected_item_path():
pass
self.createNewProject.clicked.connect(new_project_window)
This is Qlistwidget from selected directory items.
self.sceneLabel = QLabel()
self.sceneLabel.setText("Episodes: ")
self.sceneList = QListWidget()
self.sceneList.itemClicked.connect(selected_item_path)
And this is my second list widget (i want to list under the selected directories folder from scneneList Widget...
self.shotLabel = QLabel("Shots: ")
self.shotList = QListWidget()
self.topLayout = QHBoxLayout()
self.topLayout.addWidget(self.selectProject)
self.topLayout.addWidget(self.createNewProject)
self.topLayout.addWidget(self.choose_directory)
self.secondLayout = QVBoxLayout()
self.thirdLayout = QHBoxLayout()
self.thirdLayout.addWidget(self.sceneList)
self.thirdLayout.addWidget(self.shotList)
self.labels = QHBoxLayout()
self.labels.addWidget(self.sceneLabel)
self.labels.addWidget(self.shotLabel)
self.saveSceneVer = QPushButton()
self.saveSceneVer.setText("Save New Version")
self.saveSceneVer.setFixedHeight(30)
self.saveSceneVer.setStyleSheet("background-color:#00aff0")
self.openTrack = QPushButton()
self.openTrack.setText("Open Track")
self.openTrack.setFixedHeight(30)
self.openTrack.setStyleSheet("background-color:#ff5a80")
self.opButtons = QHBoxLayout()
self.opButtons.addWidget(self.saveSceneVer)
self.opButtons.addWidget(self.openTrack)
#Tab section for a lat of operations!
self.operation_tabs = QTabWidget()
tabWidget_Animation = QWidget()
optimize_for_animator_scene = QPushButton()
optimize_for_animator_scene.setText("I am an Animator")
create_render_camera = QPushButton()
create_render_camera.setText("Create Camera")
make_playblast = QPushButton()
make_playblast.setText("Playblast")
tabWidget_Animation_Layout = QGridLayout()
tabWidget_Animation.setLayout(tabWidget_Animation_Layout)
tabWidget_Animation_Layout.addWidget(optimize_for_animator_scene,0,0,0)
tabWidget_Animation_Layout.addWidget(create_render_camera, 0,1,0)
tabWidget_Animation_Layout.addWidget(make_playblast, 0,2,0)
tabWidget_render = QWidget()
tabWidget_td = QWidget()
self.operation_tabs.addTab(tabWidget_Animation,"Animation")
self.operation_tabs.addTab(tabWidget_render,"Render")
self.operation_tabs.addTab(tabWidget_td,"Compositing")
self.masterLayout = QVBoxLayout()
self.masterLayout.addLayout(self.topLayout)
self.masterLayout.addWidget(self.dir_label)
self.masterLayout.addLayout(self.secondLayout)
self.masterLayout.addLayout(self.labels)
self.masterLayout.addLayout(self.thirdLayout)
self.masterLayout.addLayout(self.opButtons)
self.masterLayout.addWidget(self.operation_tabs)
self.setLayout(self.masterLayout)
def start():
ui = projectManagerMainWindow()
ui.setWindowTitle("Project Manager")
ui.setFixedWidth(400)
ui.show(dockable=True, floating=False, area='right')
return ui
Here is script for Autodesk Maya.
I have QTabWidget, and layout with some widgets and sub layouts that I want to show in this QTabWidget tabs. what I want to do is to add this layout to the first tab (as default), and if the user moves to the next tab, I want to show the exact same layout, and to add some widgets near it.
this is the layout that I am talking about:
self.right_tab_layout = QVBoxLayout()
self.right_tab_widget = QWidget()
self.right_tab_title_label = QLabel("Select full files path:")
self.simoderev_layout = QHBoxLayout()
self.simoderev_widget = QWidget()
self.simoderev_checkbox = QCheckBox("use simoderev as base: ")
self.simoderev_combobox = QComboBox()
self.paths_label = QLabel("paths:")
self.right_tab_widget.setLayout(self.right_tab_layout)
self.simoderev_widget.setLayout(self.simoderev_layout)
self.simoderev_widget.setMaximumWidth(250)
self.simoderev_layout.addWidget(self.simoderev_checkbox)
self.simoderev_layout.addWidget(self.simoderev_combobox)
self.simoderev_layout.setContentsMargins(0,0,0,0)
self.right_tab_layout.addWidget(self.right_tab_title_label)
self.right_tab_layout.addWidget(self.simoderev_widget)
self.right_tab_layout.addWidget(self.paths_label)
is there any way to do this ?
If you just want the widgets in the tabs to look the same, you should create a custom Widget class and put an instance of that class in each tab.
Your custom widget could look like:
class CustomWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(CustomWidget, self).__init__(parent)
layout = QtGui.QVBoxLayout(self)
self.title_label = QtGui.QLabel("Select full files path:")
self.simoderev_widget = QtGui.QWidget()
simoderev_layout = QtGui.QHBoxLayout(self.simoderev_widget)
self.simoderev_checkbox = QtGui.QCheckBox("use simoderev as base: ")
self.simoderev_combobox = QtGui.QComboBox()
self.simoderev_widget.setMaximumWidth(250)
simoderev_layout.addWidget(self.simoderev_checkbox)
simoderev_layout.addWidget(self.simoderev_combobox)
simoderev_layout.setContentsMargins(0,0,0,0)
self.paths_label = QtGui.QLabel("paths:")
layout.addWidget(self.title_label)
layout.addWidget(self.simoderev_widget)
layout.addWidget(self.paths_label)
If you want them to be the same, here's a hacky solution. You should connect the currentChanged signal of your tabwidget to a slot that will move your custom widget from one tab to the other.
class MyTabWidget(QtGui.QTabWidget):
def __init__(self, parent = None):
super(MyTabWidget, self).__init__(parent)
self.subwidget = CustomWidget(self)
self.left_tab_widget = QtGui.QWidget()
self.leftLayout = QtGui.QVBoxLayout(self.left_tab_widget)
self.leftLayout.addWidget(self.subwidget)
self.right_tab_widget = QtGui.QWidget(self)
self.rightLayout = QtGui.QVBoxLayout(self.right_tab_widget)
label = QtGui.QLabel("Some additional data", self.right_tab_widget)
self.rightLayout.addWidget(label)
self.addTab(self.left_tab_widget, "Left Tab")
self.addTab(self.right_tab_widget, "Right Tab")
self.currentChanged.connect(self.onCurrentChanged)
def onCurrentChanged(self, index):
if index == 0:
self.leftLayout.addWidget(self.subwidget)
else:
self.rightLayout.addWidget(self.subwidget)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
widget = MyTabWidget()
widget.show()
app.exec_()