1

当我尝试一些选项来配对这两个脚本时,我的问题就开始了,但它只以 TypeError: init() missing 1 required positional argument: 'parent' 结束,即使我的朋友也试图提供帮助,但它以类似的错误结束,因为他有 C++ 技能

import sys
from PyQt5.QtWidgets import QTabWidget, QWidget, QToolButton, QTabBar, QApplication


class Project_Tabs(QTabWidget):

    def __init__(self):
        QTabWidget.__init__(self)
        self.tabCloseRequested.connect(lambda index: tabs.removeTab(index))
        self.setTabsClosable(True)
        self._build_tabs()


    def _build_tabs(self):

        self.insertTab(0, QWidget(), "Project 0" )

        # create the "new tab" tab with button
        self.insertTab(1, QWidget(),'')
        nb = self.new_btn = QToolButton()
        nb.setText('+') # you could set an icon instead of text
        nb.setAutoRaise(True)
        nb.clicked.connect(self.new_tab)
        self.tabBar().setTabButton(1, QTabBar.RightSide, nb)


    def new_tab(self):
        index = self.count() - 1
        self.insertTab(index, QWidget(), "Project %d" % index)
        self.setCurrentIndex(index)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    tabs = Project_Tabs()
    tabs.show()

    app.exec_()

from PyQt5.QtCore import Qt, QEvent
from PyQt5.QtWidgets import QTabBar, QTabWidget, QApplication, QLineEdit, QWidget

class EditableTabBar(QTabBar):
    def __init__(self, parent):
        QTabBar.__init__(self, parent)
        self._editor = QLineEdit(self)
        self._editor.setWindowFlags(Qt.Popup)
        self._editor.setFocusProxy(self)
        self._editor.editingFinished.connect(self.handleEditingFinished)
        self._editor.installEventFilter(self)

    def eventFilter(self, widget, event):
        if ((event.type() == QEvent.MouseButtonPress and not self._editor.geometry().contains(event.globalPos())) or (event.type() == QEvent.KeyPress and event.key() == Qt.Key_Escape)):
            self._editor.hide()
            return True
        return QTabBar.eventFilter(self, widget, event)

    def mouseDoubleClickEvent(self, event):
        index = self.tabAt(event.pos())
        if index >= 0:
            self.editTab(index)

    def editTab(self, index):
        rect = self.tabRect(index)
        self._editor.setFixedSize(rect.size())
        self._editor.move(self.parent().mapToGlobal(rect.topLeft()))
        self._editor.setText(self.tabText(index))
        if not self._editor.isVisible():
            self._editor.show()

    def handleEditingFinished(self):
        index = self.currentIndex()
        if index >= 0:
            self._editor.hide()
            self.setTabText(index, self._editor.text())

class Window(QTabWidget):
    def __init__(self):
        QTabWidget.__init__(self)
        self.setTabBar(EditableTabBar(self))
        self.addTab(QWidget(self), 'Tab One')
        self.addTab(QWidget(self), 'Tab Two')

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

我尝试的所有尝试都以错误结束:TypeError:init()缺少1个必需的位置参数:'parent'

4

1 回答 1

1

由于不指示完整的错误消息或产生它的代码,因此不可能客观地指出错误的原因,但我认为您认为加入功能只是加入代码,但不幸的是它不是真的。

在下面的代码中,我展示了这两种功能的结合:

from PyQt5.QtCore import Qt, QEvent
from PyQt5.QtWidgets import (
    QTabBar,
    QTabWidget,
    QApplication,
    QLineEdit,
    QWidget,
    QToolButton,
)


class EditableTabBar(QTabBar):
    def __init__(self, parent):
        QTabBar.__init__(self, parent)
        self._editor = QLineEdit(self)
        self._editor.setWindowFlags(Qt.Popup)
        self._editor.setFocusProxy(self)
        self._editor.editingFinished.connect(self.handleEditingFinished)
        self._editor.installEventFilter(self)

    def eventFilter(self, widget, event):
        if (
            event.type() == QEvent.MouseButtonPress
            and not self._editor.geometry().contains(event.globalPos())
        ) or (event.type() == QEvent.KeyPress and event.key() == Qt.Key_Escape):
            self._editor.hide()
            return True
        return QTabBar.eventFilter(self, widget, event)

    def mouseDoubleClickEvent(self, event):
        index = self.tabAt(event.pos())
        if index >= 0:
            self.editTab(index)

    def editTab(self, index):
        rect = self.tabRect(index)
        self._editor.setFixedSize(rect.size())
        self._editor.move(self.parent().mapToGlobal(rect.topLeft()))
        self._editor.setText(self.tabText(index))
        if not self._editor.isVisible():
            self._editor.show()

    def handleEditingFinished(self):
        index = self.currentIndex()
        if index >= 0:
            self._editor.hide()
            self.setTabText(index, self._editor.text())


class Window(QTabWidget):
    def __init__(self):
        QTabWidget.__init__(self)
        self.setTabBar(EditableTabBar(self))
        self.setTabsClosable(True)
        self.tabCloseRequested.connect(self.removeTab)

        self.addTab(QWidget(self), "Tab One")
        self.addTab(QWidget(self), "Tab Two")

        count = self.count()

        nb = QToolButton(text="+", autoRaise=True)
        nb.clicked.connect(self.new_tab)
        self.insertTab(count, QWidget(), "")
        self.tabBar().setTabButton(count, QTabBar.RightSide, nb)

    def new_tab(self):
        index = self.count() - 1
        self.insertTab(index, QWidget(), "Tab %d" % index)
        self.setCurrentIndex(index)


if __name__ == "__main__":

    import sys

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
于 2020-06-16T19:16:45.717 回答