我有一个GUI,我正在尝试制作具有不同功能的选项卡式窗口界面;加载数据,清理数据,过程数据等。
我有一个按钮,可以从列表中选择类别。我不知道如何将其移动到我的标签页。因此,当我更改选项卡时,如果我希望它位于第一个选项卡上,则对话框始终位于顶部。
我可以像其他按钮一样移动“选择类别”按钮,并在上面添加标签以显示选择内容,但是无法弄清楚如何使用addRow布局移动对话框。这可能吗?
谢谢。
下面的代码;
from __future__ import division, print_function
import sys
from PyQt4 import QtGui, QtCore
class tester(QtGui.QWidget):
def __init__(self):
# create GUI
QtGui.QMainWindow.__init__(self)
self.setWindowTitle('Data tool')
# window dimensions
self.resize(300, 75)
form = QtGui.QFormLayout()
# tabbing on
tab_widget = QtGui.QTabWidget()
tab1 = QtGui.QWidget()
tab2 = QtGui.QWidget()
self.p1_vertical = QtGui.QVBoxLayout(tab1)
self.p2_vertical = QtGui.QVBoxLayout(tab2)
tab_widget.addTab(tab1, "Data selection")
tab_widget.addTab(tab2, "Tools")
# selected dir label
self.lbl_dir = QtGui.QLabel('Data directory not selected')
self.p1_vertical.addWidget(self.lbl_dir)
# button label for datadir
btn_dir = QtGui.QPushButton('Browse...', self)
self.p1_vertical.addWidget(btn_dir)
self.connect(btn_dir, QtCore.SIGNAL('clicked()'), self.get_dirname)
# this is the bit i want to be in the tab
# get category input
self.btn_cat = QtGui.QPushButton("Select category")
self.btn_cat.clicked.connect(self.getcat)
self.le3 = QtGui.QLineEdit()
form.addRow(self.btn_cat, self.le3)
self.le3.setReadOnly(True)
# label for status of run
self.lblrun = QtGui.QLabel('Run')
self.p2_vertical.addWidget(self.lblrun)
# button for run
btn_run = QtGui.QPushButton('Run', self)
self.p2_vertical.addWidget(btn_run)
self.connect(btn_run, QtCore.SIGNAL('clicked()'), self.run)
# vertical layout for widgets
self.vbox = QtGui.QVBoxLayout()
self.vbox.addLayout(form)
self.vbox.addWidget(tab_widget)
self.setLayout(self.vbox)
def getcat(self):
cats = ['option 1', 'option 2']
category, ok = QtGui.QInputDialog.getItem(self, "select category",
"Option", cats, 0, False)
if ok:
self.le3.setText(category)
self.category = category
def get_dirname(self):
print('yes')
def run(self):
print('yes')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
gui = tester()
gui.show()
app.exec_()
看起来像这样
想要这样;
答案 0 :(得分:2)
您必须在tab1的布局中设置QFormLayout以消除以下情况:
self.p1_vertical.addLayout(form)
并添加:
self.vbox.addLayout(form)
尽管为了更好的组织,我更喜欢创建新的类:
from __future__ import division, print_function
import sys
from PyQt4 import QtGui, QtCore
class DataSelectionWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(DataSelectionWidget, self).__init__(parent)
self.btn_cat = QtGui.QPushButton("Select category")
self.btn_cat.clicked.connect(self.getcat)
self.le3 = QtGui.QLineEdit(readOnly=True)
flay = QtGui.QFormLayout(self)
flay.addRow(self.btn_cat, self.le3)
# selected dir label
self.lbl_dir = QtGui.QLabel('Data directory not selected')
flay.addRow(self.lbl_dir)
# button label for datadir
btn_dir = QtGui.QPushButton('Browse...', self)
flay.addRow(btn_dir)
btn_dir.clicked.connect(self.get_dirname)
@QtCore.pyqtSlot()
def getcat(self):
cats = ['option 1', 'option 2']
category, ok = QtGui.QInputDialog.getItem(self, "select category",
"Option", cats, 0, False)
if ok:
self.le3.setText(category)
self.category = category
@QtCore.pyqtSlot()
def get_dirname(self):
print('yes')
class ToolsWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(ToolsWidget, self).__init__(parent)
lay = QtGui.QVBoxLayout(self)
self.lblrun = QtGui.QLabel('Run')
lay.addWidget(self.lblrun)
# button for run
btn_run = QtGui.QPushButton('Run', self)
lay.addWidget(btn_run)
btn_run.clicked.connect(self.run)
@QtCore.pyqtSlot()
def run(self):
print('yes')
class Tester(QtGui.QWidget):
def __init__(self, parent=None):
# create GUI
super(Tester, self).__init__(parent)
self.setWindowTitle('Data tool')
# window dimensions
self.resize(300, 75)
tab1 = DataSelectionWidget()
tab2 = ToolsWidget()
# tabbing on
tab_widget = QtGui.QTabWidget()
tab_widget.addTab(tab1, "Data selection")
tab_widget.addTab(tab2, "Tools")
lay = QtGui.QVBoxLayout(self)
lay.addWidget(tab_widgetn)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
gui = Tester()
gui.show()
sys.exit(app.exec_())