我已经使用PyQt Designer创建了一个简单的GUI,并使其将.ui文件转换为python代码。
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(485, 785)
Dialog.setMinimumSize(QtCore.QSize(485, 400))
Dialog.setMaximumSize(QtCore.QSize(495, 785))
Dialog.setSizeIncrement(QtCore.QSize(1, 1))
Dialog.setMouseTracking(False)
Dialog.setAutoFillBackground(False)
Dialog.setStyleSheet(_fromUtf8(""))
Dialog.setModal(False)
self.layoutWidget = QtGui.QWidget(Dialog)
self.layoutWidget.setGeometry(QtCore.QRect(10, 736, 471, 49))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.SaveCancelLayout = QtGui.QHBoxLayout(self.layoutWidget)
self.SaveCancelLayout.setContentsMargins(0, 8, 5, 0)
self.SaveCancelLayout.setSpacing(10)
self.SaveCancelLayout.setObjectName(_fromUtf8("SaveCancelLayout"))
self.saveButton = QtGui.QPushButton(self.layoutWidget)
self.saveButton.setMaximumSize(QtCore.QSize(200, 40))
self.saveButton.setObjectName(_fromUtf8("saveButton"))
self.SaveCancelLayout.addWidget(self.saveButton)
self.startLogging = QtGui.QPushButton(self.layoutWidget)
self.startLogging.setMinimumSize(QtCore.QSize(170, 40))
self.startLogging.setMaximumSize(QtCore.QSize(200, 40))
self.startLogging.setIconSize(QtCore.QSize(16, 16))
self.startLogging.setCheckable(False)
self.startLogging.setChecked(False)
self.startLogging.setAutoRepeat(True)
self.startLogging.setAutoExclusive(False)
self.startLogging.setAutoRepeatDelay(300)
self.startLogging.setDefault(True)
self.startLogging.setFlat(False)
self.startLogging.setObjectName(_fromUtf8("startLogging"))
self.SaveCancelLayout.addWidget(self.startLogging)
self.stopLogging = QtGui.QPushButton(self.layoutWidget)
self.stopLogging.setMinimumSize(QtCore.QSize(0, 40))
self.stopLogging.setMaximumSize(QtCore.QSize(200, 40))
self.stopLogging.setObjectName(_fromUtf8("stopLogging"))
self.SaveCancelLayout.addWidget(self.stopLogging)
self.cancelButton = QtGui.QPushButton(self.layoutWidget)
self.cancelButton.setMaximumSize(QtCore.QSize(200, 40))
self.cancelButton.setLayoutDirection(QtCore.Qt.LeftToRight)
self.cancelButton.setObjectName(_fromUtf8("cancelButton"))
self.SaveCancelLayout.addWidget(self.cancelButton)
self.gridLayoutWidget = QtGui.QWidget(Dialog)
self.gridLayoutWidget.setGeometry(QtCore.QRect(9, 9, 471, 721))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setMargin(9)
self.gridLayout.setSpacing(6)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.commandLinkButton = QtGui.QCommandLinkButton(self.gridLayoutWidget)
self.commandLinkButton.setObjectName(_fromUtf8("commandLinkButton"))
self.gridLayout.addWidget(self.commandLinkButton, 6, 0, 1, 1)
self.checkBox_4 = QtGui.QCheckBox(self.gridLayoutWidget)
self.checkBox_4.setObjectName(_fromUtf8("checkBox_4"))
self.gridLayout.addWidget(self.checkBox_4, 6, 1, 1, 1)
self.checkBox_3 = QtGui.QCheckBox(self.gridLayoutWidget)
self.checkBox_3.setObjectName(_fromUtf8("checkBox_3"))
self.gridLayout.addWidget(self.checkBox_3, 1, 1, 1, 1)
self.buttonBox = QtGui.QDialogButtonBox(self.gridLayoutWidget)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.gridLayout.addWidget(self.buttonBox, 1, 2, 1, 1)
self.checkBox = QtGui.QCheckBox(self.gridLayoutWidget)
self.checkBox.setObjectName(_fromUtf8("checkBox"))
self.gridLayout.addWidget(self.checkBox, 0, 1, 1, 1)
self.radioButton = QtGui.QRadioButton(self.gridLayoutWidget)
self.radioButton.setObjectName(_fromUtf8("radioButton"))
self.gridLayout.addWidget(self.radioButton, 5, 0, 1, 1)
self.checkBox_2 = QtGui.QCheckBox(self.gridLayoutWidget)
self.checkBox_2.setObjectName(_fromUtf8("checkBox_2"))
self.gridLayout.addWidget(self.checkBox_2, 5, 1, 1, 1)
self.toolButton = QtGui.QToolButton(self.gridLayoutWidget)
self.toolButton.setObjectName(_fromUtf8("toolButton"))
self.gridLayout.addWidget(self.toolButton, 1, 0, 1, 1)
self.buttonBox_2 = QtGui.QDialogButtonBox(self.gridLayoutWidget)
self.buttonBox_2.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox_2.setObjectName(_fromUtf8("buttonBox_2"))
self.gridLayout.addWidget(self.buttonBox_2, 5, 2, 1, 1)
self.pushButton = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.buttonBox_3 = QtGui.QDialogButtonBox(self.gridLayoutWidget)
self.buttonBox_3.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox_3.setObjectName(_fromUtf8("buttonBox_3"))
self.gridLayout.addWidget(self.buttonBox_3, 0, 2, 1, 1)
self.buttonBox_4 = QtGui.QDialogButtonBox(self.gridLayoutWidget)
self.buttonBox_4.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox_4.setObjectName(_fromUtf8("buttonBox_4"))
self.gridLayout.addWidget(self.buttonBox_4, 6, 2, 1, 1)
self.checkBox_5 = QtGui.QCheckBox(self.gridLayoutWidget)
self.checkBox_5.setObjectName(_fromUtf8("checkBox_5"))
self.gridLayout.addWidget(self.checkBox_5, 2, 1, 1, 1)
self.radioButton_2 = QtGui.QRadioButton(self.gridLayoutWidget)
self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
self.gridLayout.addWidget(self.radioButton_2, 2, 0, 1, 1)
self.radioButton_3 = QtGui.QRadioButton(self.gridLayoutWidget)
self.radioButton_3.setObjectName(_fromUtf8("radioButton_3"))
self.gridLayout.addWidget(self.radioButton_3, 3, 0, 1, 1)
self.radioButton_4 = QtGui.QRadioButton(self.gridLayoutWidget)
self.radioButton_4.setObjectName(_fromUtf8("radioButton_4"))
self.gridLayout.addWidget(self.radioButton_4, 2, 2, 1, 1)
self.radioButton_5 = QtGui.QRadioButton(self.gridLayoutWidget)
self.radioButton_5.setObjectName(_fromUtf8("radioButton_5"))
self.gridLayout.addWidget(self.radioButton_5, 3, 2, 1, 1)
self.checkBox_6 = QtGui.QCheckBox(self.gridLayoutWidget)
self.checkBox_6.setObjectName(_fromUtf8("checkBox_6"))
self.gridLayout.addWidget(self.checkBox_6, 3, 1, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "MyWindow", None))
self.saveButton.setText(_translate("Dialog", "Button_Save", None))
self.startLogging.setText(_translate("Dialog", "Save&start", None))
self.stopLogging.setText(_translate("Dialog", "Stop", None))
self.cancelButton.setText(_translate("Dialog", "Exit", None))
self.commandLinkButton.setText(_translate("Dialog", "CommandLinkButton", None))
self.checkBox_4.setText(_translate("Dialog", "CheckBox", None))
self.checkBox_3.setText(_translate("Dialog", "CheckBox", None))
self.checkBox.setText(_translate("Dialog", "CheckBox", None))
self.radioButton.setText(_translate("Dialog", "RadioButton", None))
self.checkBox_2.setText(_translate("Dialog", "CheckBox", None))
self.toolButton.setText(_translate("Dialog", "...", None))
self.pushButton.setText(_translate("Dialog", "PushButton", None))
self.checkBox_5.setText(_translate("Dialog", "CheckBox", None))
self.radioButton_2.setText(_translate("Dialog", "RadioButton", None))
self.radioButton_3.setText(_translate("Dialog", "RadioButton", None))
self.radioButton_4.setText(_translate("Dialog", "RadioButton", None))
self.radioButton_5.setText(_translate("Dialog", "RadioButton", None))
self.checkBox_6.setText(_translate("Dialog", "CheckBox", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
现在,我想添加一个滚动条,该滚动条在调整主窗口大小时显示。换句话说:如果用户调整主窗口的大小以使内容的大小大于窗口本身的大小,则应显示滚动条。 我的GUI的更复杂的代码可以在这里找到:https://snip.dssr.ch/?efe8c61b137f0751#IGcw4iGEOTHYlXTseCc+Z7tPDDPVtgAeAPx5A4P8pnc=
有什么想法要实现吗?
答案 0 :(得分:0)
您需要使用QScrollArea
。代替
self.layoutWidget = QtGui.QWidget(Dialog)
做类似的事情
self.scrollLayout = QtGui.QVBoxLayout(Dialog)
self.scrollBox = QtGui.QScrollArea()
self.scrollLayout.addWidget(self.scrollBox)
self.mainWidget = QtGui.QWidget()
self.scrollBox.setWidget(self.mainWidget)
self.mainLayout = QtGui.QVBoxLayout(self.mainWidget)
然后,像这样的每一行
self.foo = QtGui.QWidget(Dialog)
您需要将Dialog
换成self.mainWidget
,或将其显式添加到布局中。
self.foo = QtGui.QWidget()
self.mainLayout.addWidget(self.foo)
这也可以使用Designer完成。不过我不确定。