0

I have been struggling to learn PyQt5 (and object oriented programming). In my current script I need to create a tabbed interface but can't seem to manage it. I suspect the problem is related to OOP (I am a novice). "self" seems to be the problem, and I kind of know what that means but not enough to be able to fix it. Below is my latest attempt. It seems like I am using the "wrong self", from elsewhere in the script. I want very much to understand object oriented programming - thanks in advance to anyone kind enough to help!

Some of the code/errors are:

code:

tabbar = QTabBar()  
tab1 = QTabWidget()  
tabbar.addTab(tab1, 'tab1')  

error:

TypeError: arguments did not match any overloaded call:
addTab(self, str): argument 1 has unexpected type 'QTabWidget'
addTab(self, QIcon, str): argument 1 has unexpected type 'QTabWidget'

And here's the code:

class App(QMainWindow):

    def launch(self, filepath):
        subprocess.run(filepath)


    def newLauncher(self, matrix): 
        pass  # cut for brevity


    def __init__(self):
        super(App, self).__init__()

        tabbar = QTabBar()
        tab1 = QTabWidget()
        index = tabbar.addTab(tab1, 'tab1')

        self.initUI()


    def initUI(self):

        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
tscv11
  • 55
  • 1
  • 7
  • I should mention that I spent hours trying variations on what you see above. I can't seem to find the right way to do it. For example, I tried "tabbar.addTab(tab1, 'tab1')" instead of "index = tabbar", etc. – tscv11 Mar 10 '18 at 02:16
  • Does this code not generate syntax errors? – eyllanesc Mar 10 '18 at 02:19
  • Yes it does. Hello again. I'm still trying to get beyond that point. The only things I've tried that don't cause syntax errors cause other errors. – tscv11 Mar 10 '18 at 02:22
  • You must publish all the necessary information to understand your problem and this entails the error messages, edit your question and add it there. – eyllanesc Mar 10 '18 at 02:23
  • There have been so many different errors. I'll put a few in the question. – tscv11 Mar 10 '18 at 02:25

1 Answers1

1

It is good that you want to learn about OOP, but that is not the main problem in this case, but it seems that you do not read the documentation. If we check that it is a QTabBar it would see that it refers to the top part of the QTabWidget, it is that part with buttons.

You do not have to use QTabBar but QTabWidget, QTabWidget has the addTab method that requires as a first parameter the widget that will be displayed on a page, and as a second parameter a title that will appear on the buttons.

Another mistake that I see in your code is that you create the widget but not setting it as part of another widget are just local variables that we know are deleted when the function is finished.

Since you are using QMainWindow you must set QTabWidget as part of a central widget, for this we can use the layouts.

import sys

from PyQt5.QtWidgets import *

class App(QMainWindow):
    def __init__(self):
        super(App, self).__init__()

        centralWidget = QWidget()
        lay = QVBoxLayout(centralWidget)

        tab = QTabWidget()
        lay.addWidget(tab)
        for i in range(5):
            page = QWidget()
            tab.addTab(page, 'tab{}'.format(i))

        self.setCentralWidget(centralWidget)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Reading the documentation and understanding it are not the same. I find the PyQt documentation lacking. What I have been able to find is not clear enough (to me) and does not provide many examples. Answer accepted - thanks again. – tscv11 Mar 10 '18 at 02:38
  • @tscv11 Currently most of the documentation of PyQt5 refers to Qt5, so I know the Qt5 documentation is one of the best I've seen. The external tutorials are not part of the documentation ... I'm curious: What documentation have you read? – eyllanesc Mar 10 '18 at 02:42
  • Update: the suggested code gives this error: "QWidget: Must construct a QApplication before a QWidget." As far as your other comment goes, you can choose not to believe I've read the docs, it doesn't bother me - you don't have to answer my questions so I'm not imposing. I appreciate that you have helped, though. – tscv11 Mar 10 '18 at 02:55
  • @tscv11 Have you copied all the code or have you modified it? for me it is that you have wanted to adapt it in an inadequate way. – eyllanesc Mar 10 '18 at 02:57
  • To answer your last question, I ran it as it was, nothing happened. I'm used to seeing something if there's a "show" command so I tried putting "App()" at the end, which is probably the cause for the error. I guess I expected the code to produce an example of the desired result. – tscv11 Mar 10 '18 at 03:02
  • @tscv11 share the code that you are running through dropbox, drive or similar. – eyllanesc Mar 10 '18 at 03:04