2014-09-04 35 views
1

我想在不同的選項卡中運行我的應用程序。現在,它在主窗口中運行。我已經完成了一些關於如何創建制表符的搜索工作。我發現這是有用的,但不能滿足我的要求使用TabWidget在選項卡中運行Pyqt UI

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

我想有添加新的選項卡(如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()  

在此先感謝.. :)

回答

2

它只是使用方法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是可以參考的。所以,你可以通過調用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_()) 
+0

感謝kitsune(再次..我記得你回答我以前的問題)。是的,我可以實現這一點,但問題是我處理了很多全局變量。每個選項卡都應該重新運行整個程序。是的,我可以將全局變量設爲實例變量,但這會使我的代碼更加混亂。請在此建議。 – phaigeim 2014-09-04 07:35:17

+0

因此,您想要像在Web瀏覽器中那樣自動添加小部件,並且不要在其中創建變量?我不知道我理解你的發言已更正,但請閱讀我上次編輯的答案。 – 2014-09-04 08:13:08

+1

是的,這將是有益的..我會嘗試修改我的代碼,使其工作。 – phaigeim 2014-09-04 09:28:47

相關問題