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?
Advantages of PySide PySide represents the official set of Python bindings backed up by the Qt Company. PySide comes with a license under the LGPL, meaning it is simpler to incorporate into commercial projects when compared with PyQt. It allows the programmer to use QtQuick or QML to establish the user interface.
PyQt is significantly older than PySide and, partially due to that, has a larger community and is usually ahead when it comes to adopting new developments. It is mainly developed by Riverbank Computing Limited and distributed under GPL v3 and a commercial license.
PyQt is a Python binding for Qt, which is a set of C++ libraries and development tools that include platform-independent abstractions for Graphical User Interfaces (GUI), as well as networking, threads, regular expressions, SQL databases, SVG, OpenGL, XML, and many other powerful features.
Since PyQT version 5.15 you can use setTabVisible(index, visible) . It will hide the tab at given index. When you will pass visible as true, it will show that tab and index will remain the same.
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)
The best way is to just change "1" to "0" in line:
tabs.setCurrentIndex(1)
like
tabs.setCurrentIndex(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With