用QtDesigner画主界面,在toolBar上添加QSpinBox:
step1:在界面上添加所有需要的控件,按要求修改属性值
step2:在生成的Ui_file中修改代码:
1.将self.spinBox = QtGui.QSpinBox(self.centralwidget)
修改为self.spinBox = QtGui.QSpinBox()
2.在toolBar代码后添加
self.toolBar.addWidget(self.spinBox)
3.备注:有一行代码是
紧接着:self.spinBox.setObjectName(_fromUtf8(“spinBox”))
的这一行MainWindow.setCentralWidget(self.centralwidget),
具体作用还不清楚,试着删除和保留对显示没有影响。
具体代码:
# -*- coding: utf-8 -*-
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.spinBox = QtGui.QSpinBox()
self.spinBox.setGeometry(QtCore.QRect(180, 40, 101, 22))
self.spinBox.setFocusPolicy(QtCore.Qt.NoFocus)
self.spinBox.setMinimum(1)
self.spinBox.setMaximum(400)
self.spinBox.setProperty("value", 100)
self.spinBox.setObjectName(_fromUtf8("spinBox"))
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuFile = QtGui.QMenu(self.menubar)
self.menuFile.setObjectName(_fromUtf8("menuFile"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtGui.QToolBar(MainWindow)
self.toolBar.setObjectName(_fromUtf8("toolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.actionZoom = QtGui.QAction(MainWindow)
self.actionZoom.setCheckable(True)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/icon/add.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionZoom.setIcon(icon)
self.actionZoom.setObjectName(_fromUtf8("actionZoom"))
self.menuFile.addAction(self.actionZoom)
self.menubar.addAction(self.menuFile.menuAction())
self.toolBar.addAction(self.actionZoom)
self.toolBar.addWidget(self.spinBox)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.spinBox.setToolTip(_translate("MainWindow", "Zoom the image", None))
self.spinBox.setStatusTip(_translate("MainWindow", "Zoom the image", None))
self.spinBox.setSuffix(_translate("MainWindow", "%", None))
self.menuFile.setTitle(_translate("MainWindow", "File", None))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar", None))
self.actionZoom.setText(_translate("MainWindow", "Zoom", None))
import resources_rc
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
显示结果为: