[Python] How can I remove the dotted border around the tab in focus in a multi-tabbed PyQt5...

Discussão em 'Python' iniciado por Stack, Janeiro 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have built a multi-tab window using PyQt5. Each tab has a thin dotted border when in focus:

    [​IMG]

    How do I remove this dotted border? (My operating system is Window 10 if that matters.)

    Here is the Python code that generates the above window:

    from PyQt5.QtWidgets import QTabWidget, QWidget, QVBoxLayout, QApplication
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
    from numpy.random import rand
    from sys import argv, exit

    class mainWindow(QTabWidget):
    def __init__(self, parent = None):
    super(mainWindow, self).__init__(parent)

    # Create tab1
    self.tab1 = QWidget()
    self.addTab(self.tab1,"Tab 1")
    self.figure = plt.figure()
    self.canvas = FigureCanvasQTAgg(self.figure)
    layout = QVBoxLayout()
    layout.addWidget(self.canvas)
    self.tab1.setLayout(layout)
    self.plot()

    # Create tab2
    self.tab2 = QWidget()
    self.addTab(self.tab2,"Tab 2")
    self.figure = plt.figure()
    self.canvas = FigureCanvasQTAgg(self.figure)
    layout = QVBoxLayout()
    layout.addWidget(self.canvas)
    self.tab2.setLayout(layout)
    self.plot()

    def plot(self):
    data = rand(10)
    ax = self.figure.add_subplot(111)
    ax.plot(data, '*-')
    self.canvas.draw()

    app = QApplication(argv)
    main = mainWindow()
    main.show()
    exit(app.exec_())


    Several Stack Overflow questions like this, this, and this one seem to suggest that the border might be removed by adding a line like

    self.tab1.setFocusPolicy(QtCore.Qt.NoFocus)


    or

    self.tab1.setStyleSheet("QTableView:{outline: 0;}")


    somewhere in the above code. However, I have not yet found a way to do this that succeeds in removing the dotted border.

    Continue reading...

Compartilhe esta Página