PyQt - first make one tab appear?

Quick question. I am working on a GUI in pyqt and has two tabs. Right now, the second tab is always open at startup, and I think this is because it launches a function to find the file name in QLineEdit. I would really like the first tab to appear at startup. How can i do this?

+6
source share
1 answer

If you create your user interface using Qt Creator, the tab that was active when the user interface was saved is set as the default tab. You can fix this by returning to Qt Creator, selecting the tab you want by default, and save it and recreate the .ui to .py file.

Alternatively, you can use QTabWidget setCurrentIndex(int) .

Set int equal to the index of the tab you want to display.

Example:

 from PyQt4 import QtGui from PyQt4 import QtCore import sys def main(): app = QtGui.QApplication(sys.argv) tabs = QtGui.QTabWidget() tab1 = QtGui.QWidget() tab2 = QtGui.QWidget() tab3 = QtGui.QWidget() tabs.addTab(tab1,"Tab 1") tabs.addTab(tab2,"Tab 2") tabs.addTab(tab3,"Tab 3") tabs.setWindowTitle('PyQt QTabWidget Add Tabs and Widgets Inside Tab') tabs.show() # This will set "Tab 2" to be shown when the application launches tabs.setCurrentIndex(1) sys.exit(app.exec_()) if __name__ == '__main__': main() 

This will launch a window with the active "Tab 2".

Tab 2 is active

If the line below is deleted, then "Tab 1" is active at startup

 tabs.setCurrentIndex(1) 
+9
source

Source: https://habr.com/ru/post/982970/


All Articles