Reputation: 281
Quick question - I'm working on a GUI in pyqt, and it has two tabs. Right now the second tab is always open on startup, which I think is because it runs a function to find a filename to stick in a QLineEdit. I would really like the first tab to appear on startup instead. How would I go about doing this?
Upvotes: 11
Views: 14643
Reputation: 11
The best way is to just change "1" to "0" in line:
tabs.setCurrentIndex(1)
like
tabs.setCurrentIndex(0)
Upvotes: 1
Reputation: 50600
If you build your UI using Qt Creator, the tab that was active when you saved the UI is set as the default tab. You can correct this by going back into Qt Creator, selecting that tab you want to be the default and resaving it and recreating your .ui
to .py
file.
Alternatively, you can utilize QTabWidget
s 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 that has "Tab 2" active.
If the line below is removed, then "Tab 1" is active at launch
tabs.setCurrentIndex(1)
Upvotes: 15