简体   繁体   English

PyQt5无法创建标签栏或标签

[英]PyQt5 can't create Tabbars or Tabs

I have been struggling to learn PyQt5 (and object oriented programming). 我一直在努力学习PyQt5(和面向对象的编程)。 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). 我怀疑问题与OOP有关(我是新手)。 "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_())

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. 您想了解OOP很好,但是在这种情况下,这不是主要问题,但似乎您没有阅读本文档。 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. 如果我们检查它是否为QTabBar ,则会看到它指向QTabWidget的顶部,即带有按钮的那部分。

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. 您不必使用QTabBarQTabWidgetQTabWidgetaddTab需要作为第一个参数,将在页面上显示的窗口小部件,并作为第二个参数,将出现在按钮的标题方法。

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. 由于您使用的是QMainWindow ,因此必须将QTabWidget设置为中央窗口小部件的一部分,为此,我们可以使用布局。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM