Search code examples
pythonpathpyqtqtabwidget

PyQt TabWidget not displaying ui or accepting setCurrentWidget


I've been working on an application for work and have run into a bit of a quandary. The following code will successfully add a new tab to my tabWidget, set the title of the tab, load an external ui, generate a file or preview a file when one of two buttons is pressed, and will be set to the current tab when it opens from a menu acion:

def strap6_gui(self): #Create new tab and load TMS6 Strapping UI
    self.strap6 = QtGui.QWidget(self.tabWidget)
    self.tabWidget.addTab(self.strap6, 'TMS6 Tank Strapping')
    uic.loadUi(strap6, self.strap6)
    self.strap6.strap6_generate.clicked.connect(self.strap6_func)
    self.strap6.strap6_previewButton.clicked.connect(self.strap6_prev)
    self.tabWidget.setCurrentWidget(self.strap6)

The problem is this: the following code, which was copied from the above and modified slightly, doesn't do what it's supposed to. It will open a new tab and set the title, but this new tab doesn't get set to the current tab or load the ui file:

def txled_gui(self): #Create new tab and load TMS6 Strapping UI
    self.txled = QtGui.QWidget(self.tabWidget)
    self.tabWidget.addTab(self.txled, 'TMS6 Tank Strapping')
    uic.loadUi(txui, self.txled)
    self.txled.strap6_generate.clicked.connect(self.strap6_func)
    self.txled.strap6_previewButton.clicked.connect(self.strap6_prev)
    self.tabWidget.setCurrentWidget(self.txled)

When I was working on this, I copied the strap6.ui and renamed it as txui.ui so I could tell if the tab was getting the information from the file location. The file locations are set as:

strap6 = os.path.abspath('.\UI\strap6_gui.ui')
txui = os.path.abspath('.\UI\txled_gui.ui')

I have a few other UI's that get loaded as popups that are in the same directory and called in the same fashion, so I know that cannot be the problem. Finally, here is the error messaging that shows up in the console when I run the application:

IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\Users\\Seth\\Documents\\TSE\\pythonTesting\\TriadGUI\\UI\txled_gui.ui'

How come the first def works without fail but the second one, which was a copy of the first and setup w/ new names etc. doesn't?


Solution

  • You're going to kick yourself, I think.

    txui has a tab in it because you did not escape the backslashes.

    So you need:

    txui = os.path.abspath(r'.\UI\txled_gui.ui')
    

    or:

    txui = os.path.abspath('.\\UI\\txled_gui.ui')