我正在尝试创建一个PyQt应用程序,它同时具有状态栏和菜单栏以及窗口中的其他小部件。下面是我用类QtGui.QMainWindow
方法运行它的代码。但当我打算添加更多功能时,我意识到我必须使用QtGui.QWidget
来代替
代码如下:
import sys
from PyQt4 import QtGui, QtCore
### How can I use QtGui.QWidget here??? ###
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> Window widget')
exitAction = QtGui.QAction(QtGui.QIcon('exit-icon-2.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit/Terminate application')
exitAction.triggered.connect(QtGui.qApp.quit)
self.statusBar()
menubar = self.menuBar()
menubar.setToolTip('This is a <b>QWidget</b> for MenuBar')
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
toolbar = self.addToolBar('Exit')
toolbar.addAction(exitAction)
qbtn = QtGui.QPushButton('Quit', self)
qbtn.setToolTip('This is a <b>QPushButton</b> widget')
qbtn.clicked.connect(self.launchAAA)
qbtn.resize(qbtn.sizeHint())
qbtn.move(170, 190)
self.setGeometry(500, 180, 400, 400)
self.setWindowTitle('Quit button with Message')
self.show()
def launchAAA(self, event):
reply = QtGui.QMessageBox.question(self, 'Message',
"Are you sure to quit?", QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
QtGui.QApplication.quit()
else:
pass
def main():
app = QtGui.QApplication(sys.argv)
ex=Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我的印象是菜单栏和标题栏可以使用QWidget方法创建,但在这里它不起作用。我打算使用QtGui.QLCDNumber
向应用程序添加一个LCD函数。
关于如何解决上述问题的任何建议。谢谢
您还可以使用QMainWindow和QWidget的组合。
我发现这在某些情况下很有用。您可以将statusbar和menubar添加到MainWindow部分,将widgets添加到QWidget区域。
下面是一个使用您的代码的工作解决方案。我把一个
centralWidget
和一个centralLayout
添加到QMainWindow
中,它现在保存您的qbtn
:您可以将按钮/标签等移动到一个
QWidget
,然后将这个小部件添加到主窗口。下面是它的外观(我更改了导入,使其可读性更强)。您的内容小部件类:
将其添加到主窗口:
一切都像以前一样工作,只是你的新产品中间有一个
QWidget
,而不是QMainWindow
。希望这有帮助!相关问题 更多 >
编程相关推荐