【问题标题】:Running Pyqt UIs in tabs using TabWidget使用 QTabWidget 在选项卡中运行 Pyqt UI
【发布时间】:2014-09-04 06:00:00
【问题描述】:

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

Create TAB and create textboxes that take data in the TAB-page

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

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()    

提前谢谢.. :)

【问题讨论】:

    标签: user-interface tabs pyqt4


    【解决方案1】:

    只是使用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_())
    

    【讨论】:

    • 感谢 kitsune(再次......我记得你也回答了我之前的问题)。是的,我可以实现它,但问题是我要处理很多全局变量。每个选项卡都应该重新运行整个程序。是的,我可以将全局变量设置为实例变量,但这会使我的代码更加混乱。请对此提出建议。
    • 所以您想像在 Web 浏览器中一样在选项卡中自动添加小部件,而不是在其中创建变量?我不知道我是否理解您的说法已更正,但请阅读我上次编辑的答案。
    • 是的,这会很有帮助。我会尝试修改我的代码以使其正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2013-07-25
    • 2015-06-16
    相关资源
    最近更新 更多