在 PyQt4 应用程序中创建带有选项卡的框架
creating frames with tabs in a PyQt4 app
所以基本上在某人的帮助下,我能够想出用于创建具有两个框架的 GUI 的代码。一个在左边,一个在右边。但是我在为真实应用程序编写相同代码时遇到了麻烦。
即这里的代码非常基础,因为它只包含一个主要功能。我如何像我在这里尝试的那样参考 self 重写这段代码。
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt4 Tuts")
self.home()
def home(self):
mainWindow = QtGui.QWidget()
mainLayout = QtGui.QGridLayout(mainWindow)
frameLeft = QtGui.QFrame(mainWindow)
frameLeft.setFrameShape(QtGui.QFrame.StyledPanel)
frameLeft.setFrameShadow(QtGui.QFrame.Raised)
gridLayoutLeft = QtGui.QGridLayout(frameLeft)
mainLayout.addWidget(frameLeft, 0, 0, 1, 1, QtCore.Qt.AlignVCenter)
frameRigth = QtGui.QFrame(mainWindow)
frameRigth.setFrameShape(QtGui.QFrame.StyledPanel)
frameRigth.setFrameShadow(QtGui.QFrame.Raised)
gridLayoutRigth = QtGui.QGridLayout(frameRigth)
mainLayout.addWidget(frameRigth, 0, 1, 1, 1, QtCore.Qt.AlignVCenter)
tabs = QtGui.QTabWidget()
gridLayoutRigth.addWidget(tabs, 0, 0, 1, 1, QtCore.Qt.AlignVCenter)
button = QtGui.QPushButton('test')
gridLayoutLeft.addWidget(button, 0, 0, 1, 1, QtCore.Qt.AlignVCenter)
# Create tabs
tab1 = QtGui.QWidget()
tab2 = QtGui.QWidget()
# Set layout of first tab
vBoxlayout = QtGui.QVBoxLayout()
pushButton1 = QtGui.QPushButton("Start")
pushButton2 = QtGui.QPushButton("Settings")
pushButton3 = QtGui.QPushButton("Stop")
vBoxlayout.addWidget(pushButton1)
vBoxlayout.addWidget(pushButton2)
vBoxlayout.addWidget(pushButton3)
tab1.setLayout(vBoxlayout)
# Add tabs
tabs.addTab(tab1, "Tab 1")
tabs.addTab(tab2, "Tab 2")
mainWindow.show()
self.show()
def close_application(self):
print("Whooaa so custom!")
sys.exit()
def run():
app=QtGui.QApplication(sys.argv)
GUI=Window()
sys.exit(app.exec_())
run()
我的主要问题是这里我有 self.show() 但也有 mainWindow.show() 我不知道该使用哪一个或如何消除一个所以我只需要一个。
开始工作:
mainWindow.show() # not this, this is a "QWidget"
self.show() # use this, this is a "QMainWindow"
您想要 "show" 您刚刚实施的 QMainWindow
、class。
此外,此 class 需要您在 mainwindow
中正确收集的内容,因此请使用 setCentralWidget()
:
self.setCentralWidget(mainWindow)
self.show()
最后 close_application()
永远不会被调用,您可以将它连接到一个按钮:
pushButton3.clicked.connect(self.close_application)
或覆盖closingEvent:
def closeEvent ( self, QCloseEvent ) :
print("Whooaa so custom, that is closing now!")
#QCloseEvent.accept()
所以基本上在某人的帮助下,我能够想出用于创建具有两个框架的 GUI 的代码。一个在左边,一个在右边。但是我在为真实应用程序编写相同代码时遇到了麻烦。
即这里的代码非常基础,因为它只包含一个主要功能。我如何像我在这里尝试的那样参考 self 重写这段代码。
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt4 Tuts")
self.home()
def home(self):
mainWindow = QtGui.QWidget()
mainLayout = QtGui.QGridLayout(mainWindow)
frameLeft = QtGui.QFrame(mainWindow)
frameLeft.setFrameShape(QtGui.QFrame.StyledPanel)
frameLeft.setFrameShadow(QtGui.QFrame.Raised)
gridLayoutLeft = QtGui.QGridLayout(frameLeft)
mainLayout.addWidget(frameLeft, 0, 0, 1, 1, QtCore.Qt.AlignVCenter)
frameRigth = QtGui.QFrame(mainWindow)
frameRigth.setFrameShape(QtGui.QFrame.StyledPanel)
frameRigth.setFrameShadow(QtGui.QFrame.Raised)
gridLayoutRigth = QtGui.QGridLayout(frameRigth)
mainLayout.addWidget(frameRigth, 0, 1, 1, 1, QtCore.Qt.AlignVCenter)
tabs = QtGui.QTabWidget()
gridLayoutRigth.addWidget(tabs, 0, 0, 1, 1, QtCore.Qt.AlignVCenter)
button = QtGui.QPushButton('test')
gridLayoutLeft.addWidget(button, 0, 0, 1, 1, QtCore.Qt.AlignVCenter)
# Create tabs
tab1 = QtGui.QWidget()
tab2 = QtGui.QWidget()
# Set layout of first tab
vBoxlayout = QtGui.QVBoxLayout()
pushButton1 = QtGui.QPushButton("Start")
pushButton2 = QtGui.QPushButton("Settings")
pushButton3 = QtGui.QPushButton("Stop")
vBoxlayout.addWidget(pushButton1)
vBoxlayout.addWidget(pushButton2)
vBoxlayout.addWidget(pushButton3)
tab1.setLayout(vBoxlayout)
# Add tabs
tabs.addTab(tab1, "Tab 1")
tabs.addTab(tab2, "Tab 2")
mainWindow.show()
self.show()
def close_application(self):
print("Whooaa so custom!")
sys.exit()
def run():
app=QtGui.QApplication(sys.argv)
GUI=Window()
sys.exit(app.exec_())
run()
我的主要问题是这里我有 self.show() 但也有 mainWindow.show() 我不知道该使用哪一个或如何消除一个所以我只需要一个。
开始工作:
mainWindow.show() # not this, this is a "QWidget"
self.show() # use this, this is a "QMainWindow"
您想要 "show" 您刚刚实施的 QMainWindow
、class。
此外,此 class 需要您在 mainwindow
中正确收集的内容,因此请使用 setCentralWidget()
:
self.setCentralWidget(mainWindow)
self.show()
最后 close_application()
永远不会被调用,您可以将它连接到一个按钮:
pushButton3.clicked.connect(self.close_application)
或覆盖closingEvent:
def closeEvent ( self, QCloseEvent ) :
print("Whooaa so custom, that is closing now!")
#QCloseEvent.accept()