Explain how to make the tab that is used to close. (Something similar as in a browser) to add a cross to close (if it is really done)

from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import * import sys def center(): qr = root.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) root.move(qr.topLeft()) app = QApplication(sys.argv) root= QWidget() hbox = QHBoxLayout(root) topleft = QFrame(root) topleft.setFrameShape(QFrame.StyledPanel) topleft.resize(10,0) topright = QFrame(root) topright.setFrameShape(QFrame.StyledPanel) splitter1 = QSplitter(Qt.Horizontal) splitter1.addWidget(topleft) splitter1.addWidget(topright) hbox.addWidget(splitter1) hbox1 = QHBoxLayout() tab = QTabWidget() First_frame_of_the_Right_frame = QFrame() tab.addTab(First_frame_of_the_Right_frame, "Основа") # Π²ΠΊΠ»Π°Π΄ΠΊΠΈ Second_frame_of_the_Right_frame = QFrame() tab.addTab(Second_frame_of_the_Right_frame, "Π”ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ") hbox1.addWidget(tab) topright.setLayout(hbox1) root.setLayout(hbox) root.setFixedSize(1366,720) center() root.setWindowTitle('QSplitter') menubar = QMenuBar() closefile = QAction( 'Π—Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ', root) closefile.triggered.connect(lambda : Second_frame_of_the_Right_frame.close()) fileMenu_file = menubar.addMenu('File') fileMenu_file.addAction(closefile) hbox.setMenuBar(menubar) root.show() sys.exit(app.exec_()) 

enter image description here

  • It closes with you, you just do not see it. After all, the frame is empty. Add a widget to it, then it will be visible. - mkkik
  • @mkkik But should it not work, how can the type of completely close this frame type remain just the tab of the base? - Twiss
  • And why? You close the frame, not the tab. - mkkik
  • one
    closefile.triggered.connect(lambda : tab.removeTab(1)) 1 is the tab number (numbering from scratch). - mkkik
  • one
    Not. And this is not an ID, but a sequence number. - mkkik

1 answer 1

An example of how to close the active tab on an event, as well as how to add close buttons for each tab.

 from PyQt5.QtWidgets import QWidget, QApplication, QTabWidget,\ QVBoxLayout, QMenuBar, QAction class Window(QWidget): def __init__(self): super().__init__() self.tabwdg = QTabWidget() self.tabwdg.setTabsClosable(True) # Π²ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ отобраТСния ΠΊΠ½ΠΎΠΏΠΎΠΊ закрытия self.tabwdg.addTab(QWidget(), 'first') self.tabwdg.addTab(QWidget(), 'second') self.tabwdg.tabCloseRequested.connect(self.closeTab) # связываниС сигнала наТатия Π½Π° "крСстик" с ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠΌ box = QVBoxLayout() box.addWidget(self.tabwdg) self.setLayout(box) bar = QMenuBar(self) menu = bar.addMenu('File') action = QAction('Close activ tab', self) menu.addAction(action) action.triggered.connect(self.closeActivTab) def closeActivTab(self): activ_tab_ind = self.tabwdg.currentIndex() self.closeTab(activ_tab_ind) def closeTab(self, ind): # ΠΌΠ΅Ρ‚ΠΎΠ΄ Π·Π°ΠΊΡ€Ρ‹Π²Π°Π΅Ρ‚ Π²ΠΊΠ»Π°Π΄ΠΊΡƒ ΠΏΠΎΠ΄ Π½ΠΎΠΌΠ΅Ρ€ΠΎΠΌ ind self.tabwdg.removeTab(ind) if __name__ == '__main__': import sys app = QApplication(['']) w = Window() w.resize(300, 300) w.show() sys.exit(app.exec_()) 
  • But how can I do something so that there is a button on the tab that looks like closing it? - Twiss
  • @ Dmitry Updated the answer. - mkkik