Basic tutorial on Tab closing and adding in QTabWidget!

tags: Pyqt5

QTabWidget is one of the more widely used containers in PyQt5, and it is often used in everyday software; QTabwidget is composed of several tags, and each tag can be used as an interface. The following is a simple example of applying Qtabwidget:

1.jpg

Tab1 and Tab2 are two labels above. When you click Tab1, you will enter a Tab1 interface. There are three text labels on the interface: tab1_lable1, tab2_label2 and tab3_label3; Tab2 is also the button entry of another interface.

The tab (Tab) in QTabwidget can also be opened and closed by a button; to achieve this function, two steps are required: 1. Add a close button to each tab (Tab), through **tabWidget.setTabsClosable(True) **Mechanism implementation, 2. Add opening function (opening is for each Tab) and closing function (closing is unified for all Tabs) to the tab (Tab). The opening and closing functions are based on signals in Qt groove. The display effect is as follows (only the closing function is shown here):

image_show.gif


Use Qt designer to create UI files

The interface of Qtabwidget can be implemented by code or made by Qt designer; here takes Qt designer as an example, first open Qt designer, create a Widget in the main window, then find the navigation bar on the left, and find Tab wdiget in Containers Control, after selecting this control, drag it to the main window Widget;

2.jpg

To adjust the layout of TabWidget, right click on the edge of TabWidget in TabWidget and select **Lay out -> Lay Out in a Grid ** mode, so that the control can be adjusted adaptively with the size of the window.

Snipaste_2020-01-27_12-05-36.jpg

Tab widget How to add the close button in the Tab in the tab: Use the mouse to select TabWidget and find it in the right navigation barProperty Editor -> QTabWidget -> tabsClosable, Tick the dialog box behind tabsClosable, and then a close button will appear in the upper right corner of the Tab (label)×icon

Snipaste_2020-01-27_13-45-53.jpg

Still need toTab1 Add two buttons to the windowtab_3withtab_4Two buttons to control the label behindtab3 with tab4 Open; adding the button function is relatively simple, you can directly add it from the left navigation bar, you can use the right property editor to the buttonText size, text fontto modify.


Snipaste_2020-01-27_13-50-36.jpg

The number of tabs (Tab) in the above is too small, two new tags are addedtab3、tab4 To correspond to the two buttons already created aboveStart of tab_3 and tab_4; The method of adding is as follows, don't forget to modify the name of the label after adding

Snipaste_2020-01-27_14-40-36.jpg

After the entire UI interface is created, you need to save the interface and save the method:File -> Save As…->your directory

Snipaste_2020-01-27_14-45-21.jpg

After saving, usepyqt middle pygui ToolbaruiThe ending file is converted intopyFile, here, the Pygui file has been configured in Pycharm, you can use it directly by clicking on Pycharm directly. For specific usage, please refer to this article:Qt Designer's environment configuration tutorial in Pycharm!

Snipaste_2020-01-27_14-51-03.jpg

The py file generated in the previous step cannot be used directly. When using it, you need to add a few lines of code at the end of the file to create the generation entry:

if  __name__ =='__main__':
    import sys
    from PyQt5.QtWidgets import  QApplication,QWidget
    app = QApplication(sys.argv)
    ui_mai = Ui_Form()
    mai_dow = QWidget()
    ui_mai.setupUi(mai_dow)
    mai_dow.show()
    app.exec_()

usePycharmStart the script, the previously created interface can be displayed, but no functions are added at present, and there will be no response after the button of the entire interface is clicked.

Snipaste_2020-01-27_15-07-54.jpg

The interface adds functions, divided into two parts:
  • 1. Tab (Tab) in TabWidget add close function;

  • 2. Buttontab_3、tab_4 Add a signal slot, the link function isTab 3 、Tab 4Label opening

For the first function, Tabwidget in Qt can be usedtabCloseRequested To achieve: first create a close tag (usingremove(tab)) Function, and then use tabwidgettabCloseRequested to fulfill

#Related code
#tabWidget add a signal slot to link all Tabs;
self.tabWidget.tabCloseRequested.connect(self.close_tab)

#tab (tab) close the function;
def close_tab(self,index):
      self.tabWidget.removeTab(index)

For the second function, the button adds an information slot: first create two functions to implement tabwidget to add the tab function (using theTabwidget's addTab function), and then put twopushbutton Linked to these two functions (using the information slot), the code is as follows:

#Link signal slot
self.pushButton.clicked.connect(self.add_tab_3)
self.pushButton_2.clicked.connect(self.add_tab_4)



#Two functions to add Tab
def add_tab_3(self):
    '''Join Tab 3'''
    tab_3 = QtWidgets.QWidget()
    tab_3.setObjectName("tab_3")
    self.tabWidget.addTab(tab_3,'Tab 3')
    
def add_tab_4(self):
    '''Join Tab 4'''
    tab_4 = QtWidgets.QWidget()
    tab_4.setObjectName('tab_4')
    self.tabWidget.addTab(tab_4,'Tab 4')

Here the tab (tab) in QTabWidget is closed, the opening function is basically completed, you can see the effect

end_imag.gif

I have put the complete code of this tutorial into Github, and those who are interested can check it through this link:Github


I focus on data analysis, data visualization, and image processing. My personal account number: Mr. Z, please do it often!

Intelligent Recommendation

QTabWidget Tab Item One reason

The main menu of a small project uses QTabWidget, and I have never click on the Tab item. I have been working for a long time. I found that QSS is provoked. The unscrupulous QSS compilation has caused...

【Qt】 Qtabwidget hide a certain TAB

First, call QTABWIDGET QTABWIDGET :: Settabenabled (Int Internet, BOOL ENABLE) interface will need hidden TAB to close; At this time, the tab clicks in the interface did not respond, but you can still...

pyqt5 QTabWidget adjusts tab width

pyqt5 Use setStyleSheet to set the border style operation of a cell   A recent project was to use python to develop a GUI graphical interactive interface for electronic scales. One page needs to ...

Set the tab tab of QTabWidget to vertical in Qt

When designing an interface, you need to use widgets similar to the word bond style. I thought of the TabWidget widget. As shown in the figure, each tab page can be connected to a widget. By setting t...

QT's qss tutorial-QTabWidget

1. Normal mode, as shown in the figure 2. The frame of the selected label is enlarged, as shown in the figure 3. The label is centered, as shown in the figure...

More Recommendation

Hongmeng Tablist and Tab Basic Tutorial

Preface: Hello everyone, there is no more than a long time, I don't remember, I don't remember that I have written some small Demo, I have to share some small Demo, I have to share some of TabList and...

Qt study notes (eight) tab window (QTabWidget)

1, basic use Also create a window-derived class that inherits from QWidget and add a slot event: Link slot Add and remove tabs...

Disable tab label switching roller QTabWidget

Recent project encountered a problem and needs to disable the scroll wheel to switch QTabWidget the Tab label, QTabWidget itself is not open interface settings to disable the wheel; I think of the fil...

QTabWidget tab bar to add a + (plus sign)

Encountered a small problem today, we need to add the + symbol on the label TabWidget achieve increased tab click the + Can be achieved by the leftmost Qt comes QTabWidget method :: topLeftCorner righ...

QTabWidget dynamically add empty label Tab

Now you need to create several empty Tab pages of TabWidget through code. So you need to use the addTab function, butint QTabWidget::addTab(QWidget *page, const QString &label) The first parameter...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top