1

I am trying to figure out how I can implement my code to allow me to create a tab based on a string list and output each tab with the correct corresponding parameters/data for that tab.

A little background on this program before we keep going. The program is a text/log comparison tool I use to compare many configurations for devices against a default/correct configuration. If there are any parameters that are incorrect with the device when compared it will output the incorrect parameters to let me/user know.

But for the sake of this portion of the code lets just say we're comparing some animals.

Heres a preview of what I currently have has a GUI/Program: GUI Preview

Essentially my goal is to have the <Cat> <Dog> <Bird> <Tiger> all within their own tab with their data.

The code I have been working with is the following:

 with open(compareResults, 'wb') as fdout:
            while (count < (len(setNames))):
                for number, item in enumerate(lst, 0):
                    print setNames[count]
                    fdout.write('{}'.format(setNames[count]) + '\r\n')
                    for param, correct in correct_parameters.items():
                        if item.get(param) != correct:
                            print ('{} = {}'.format(param, item.get(param)))
                            fdout.write('{} = {}'.format(param, item.get(param)) + '\r\n')
                    count += 1

The List that holds each name for each animal is setNames[] If you would like to view the whole code click on the pastebin link; http://pastebin.com/6v86qgj3

My idea was to, somehow, create a loop with setNames that will allow me to make a tab for each name in setNames and then output the results to each corresponding name. However, I am not that good with PyQt and I am still trying to figure out how to make individual tabs and if I can even create this without first having to make a function for the tabs or something in this nature. Any help would be much appreciated.

1 Answer 1

1

First of all apologies, I only have PySide installed here but it shouldn't make a difference. Here's an example of a widget that has tabs created by an array:

from PySide import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self, parent = None):
        super(Window, self).__init__(parent)

        # Create a QTabWidget where all the tabs will be held
        self.myTabs = QtGui.QTabWidget()

        # Loop through our animal names
        setNames = ['Cat', 'Dog', 'Bird', 'Tiger']
        for tabName in setNames:
            # Each tab needs its own widget, so let's create one with a label
            tabWidget = QtGui.QWidget()
            tabLabel = QtGui.QLabel('Hello! I am a {0}.'.format(tabName) )

            # Set the tab widget's layout
            tabLayout = QtGui.QVBoxLayout()
            tabLayout.addWidget(tabLabel)
            tabWidget.setLayout(tabLayout)

            # Create the tab
            self.myTabs.addTab(tabWidget, tabName)

        # Set the main layout
        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.myTabs)
        self.setLayout(mainLayout)

        # Resize our window and show it
        self.resize(300, 300)
        self.show()

win = Window()

You can see it's fairly straight forward. If you ever need to access a tab's widget again, you can use QTabWidget.currentWidget() or QTabWidget.widget(<index>).

3
  • Hi, Your code works fine on standalone but when trying to implement it into my own I get this error AttributeError: 'QWidget' object has no attribute 'addTab' But we're on the right track
    – JeanP
    Commented Sep 9, 2015 at 14:30
  • Be careful that you're not trying to run addTab() on a QtGui.QWidget, but on a QtGui.QTabWidget
    – Green Cell
    Commented Sep 9, 2015 at 15:25
  • Okay that fixed the issue. Now it's a matter of where to place the code within my loop so I can correctly print out the results
    – JeanP
    Commented Sep 9, 2015 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.