使用TabWidget在选项卡中运行Pyqt UI



我想在不同的选项卡中运行我的应用程序。现在,它正在主窗口中运行。我已经做了一些关于如何创建选项卡的搜索工作。我发现这是有用的,但不足以满足我的要求

创建TAB并创建在TAB页面中获取数据的文本框

我想要添加一个新标签的功能(比如chrome中的新标签)下面是我的代码示例。我在评论中描述了我的要求。

from PyQt4 import Qt, QtCore, QtGui
import sys
class Map(QtGui.QMainWindow):
def __init__(self,parentQExampleScrollArea=None,parentQWidget = None):
    super(Map,self).__init__()
    self.initUI()

#Initialize the UI
def initUI(self):
    #Initilizing Menus toolbars
    #This must be maintained in all tabbed panes
    filename = ""
    #Filename is obtained through open file button in file menu
    self.filename = filename
def paintEvent(self, e):
    qp = QtGui.QPainter()
    qp.begin(self)
    self.drawPoints(qp,self.filename)
    qp.end()
def drawPoints(self, qp,FILENAME=""):
    #Read contents in file
    #Get the necessary coordinates
    #A separate class for storing the info of all the coordinates
    #Run a for loop for all the coordinates in the list
    #Actually, object is created here and image of that object is set
    # as  a square using the coordinates
    qp.setBrush(QtGui.QColor(255, 0, 20, 200))        
    qp.drawRect(20,20,75,75)
    qp.drawRect(100,20,75,75)
    self.update()
    #There are many functions to handle keyboard and mouse events

def main():
#How do I modify so that I can have multiple tabs
#And show images of the coordinates in files
#Basically I want to have the feature of opening many files
# and displaying them in UI
#Basically a feature to add a new tab
#like that of in eclipse netbeans sublime etc
app = QtGui.QApplication(sys.argv)
myQExampleScrollArea = Map()
myQExampleScrollArea.show()
sys.exit(app.exec_())
if __name__ == '__main__':
   main()    

提前感谢..:)

只需使用方法int QTabWidget.addTab(self,QWidget widget,QString)在选项卡中创建小部件。在每个选项卡中,我建议使用QtGui.QWidget而不是QtGui.QMainWindow

示例;

import sys
from PyQt4 import QtGui
class QCustomWidget (QtGui.QWidget):
    # Your widget to implement
    # Put your override method here
    def paintEvent (self, eventQPaintEvent):
        currentQPainter = QtGui.QPainter()
        currentQPainter.begin(self)
        currentQPainter.setBrush(QtGui.QColor(255, 0, 20, 200))        
        currentQPainter.drawRect(20, 20, 75, 75)
        currentQPainter.drawRect(100, 20, 75, 75)
        self.update()
        currentQPainter.end()
class QCustomTabWidget (QtGui.QTabWidget):
    def __init__ (self, parent = None):
        super(QCustomTabWidget, self).__init__(parent)
        self.addTab(QtGui.QPushButton('Test'), 'Tab 1')
        self.addTab(QCustomWidget(),           'Tab 2')
myQApplication = QtGui.QApplication([])
myQCustomTabWidget = QCustomTabWidget()
myQCustomTabWidget.show()
sys.exit(myQApplication.exec_())

所以用更多的选项卡处理,创建多行调用int QTabWidget.addTab (self, QWidget widget, QString)是不好的。无论如何,QTabWidget中的所有小部件都可以在ifself中引用。因此,您可以通过调用QWidget QTabWidget.widget (self, int index)来控制其中的元素。

选项卡窗口小部件中调用窗口小部件的示例;

import os
import sys
from PyQt4 import QtCore, QtGui
class QCustomLabel (QtGui.QLabel):
    def __init__(self, imagePath, parentQWidget = None):
        super(QCustomLabel, self).__init__(parentQWidget)
        self.setPixmap(QtGui.QPixmap(imagePath))
class QCustomWidget (QtGui.QWidget):
    def __init__ (self, parentQWidget = None):
        super(QCustomWidget, self).__init__(parentQWidget)
        self.addQPustButton = QtGui.QPushButton('Open image')
        self.addQPustButton.setMaximumWidth(120)
        self.addQPustButton.released.connect(self.openImage)
        self.workSpaceQTabWidget = QtGui.QTabWidget()
        self.workSpaceQTabWidget.setTabsClosable(True)
        self.workSpaceQTabWidget.tabCloseRequested.connect(self.closeImage)
        allQVBoxLayout = QtGui.QVBoxLayout()
        allQVBoxLayout.addWidget(self.addQPustButton)
        allQVBoxLayout.addWidget(self.workSpaceQTabWidget)
        self.setLayout(allQVBoxLayout)
    def openImage (self):
        path = QtGui.QFileDialog.getOpenFileName(self, 'Open image')
        if not path.isEmpty():
            self.workSpaceQTabWidget.addTab(QCustomLabel(path), QtCore.QString(os.path.basename(str(path))))
    def closeImage (self, currentIndex):
        currentQWidget = self.workSpaceQTabWidget.widget(currentIndex)
        currentQWidget.deleteLater()
        self.workSpaceQTabWidget.removeTab(currentIndex)
myQApplication = QtGui.QApplication([])
myQCustomWidget = QCustomWidget()
myQCustomWidget.show() 
sys.exit(myQApplication.exec_())

最新更新