Skip to content

Instantly share code, notes, and snippets.

@LegoStormtroopr
Last active January 23, 2020 09:42
Show Gist options
  • Save LegoStormtroopr/5075267 to your computer and use it in GitHub Desktop.
Save LegoStormtroopr/5075267 to your computer and use it in GitHub Desktop.
'FingerTabs' - Horizontal Text, Horizontal Tabs in PyQt This [trivial fingertab gist](https://gist.github.com/LegoStormtroopr/5075267) is released as Public Domain, but boy would it beswell if you could credit me, or tweet me [@LegoStormtoopr](http://www.twitter.com/legostormtroopr) to say thanks!
from PyQt4 import QtGui, QtCore
from FingerTabs import FingerTabWidget
import sys
app = QtGui.QApplication(sys.argv)
tabs = QtGui.QTabWidget()
tabs.setTabBar(FingerTabBarWidget(width=100,height=25))
digits = ['Thumb','Pointer','Rude','Ring','Pinky']
for i,d in enumerate(digits):
widget = QtGui.QLabel("Area #%s <br> %s Finger"% (i,d))
tabs.addTab(widget, d)
tabs.setTabPosition(QtGui.QTabWidget.West)
tabs.show()
sys.exit(app.exec_())
# Updated so a PyQT4 Designer TabWidget can be promoted to a FingerTabWidget
from PyQt4 import QtGui, QtCore
class FingerTabBarWidget(QtGui.QTabBar):
def __init__(self, parent=None, *args, **kwargs):
self.tabSize = QtCore.QSize(kwargs.pop('width',100), kwargs.pop('height',25))
QtGui.QTabBar.__init__(self, parent, *args, **kwargs)
def paintEvent(self, event):
painter = QtGui.QStylePainter(self)
option = QtGui.QStyleOptionTab()
for index in range(self.count()):
self.initStyleOption(option, index)
tabRect = self.tabRect(index)
tabRect.moveLeft(10)
painter.drawControl(QtGui.QStyle.CE_TabBarTabShape, option)
painter.drawText(tabRect, QtCore.Qt.AlignVCenter |\
QtCore.Qt.TextDontClip, \
self.tabText(index));
painter.end()
def tabSizeHint(self,index):
return self.tabSize
# Shamelessly stolen from this thread:
# http://www.riverbankcomputing.com/pipermail/pyqt/2005-December/011724.html
class FingerTabWidget(QtGui.QTabWidget):
"""A QTabWidget equivalent which uses our FingerTabBarWidget"""
def __init__(self, parent, *args):
QtGui.QTabWidget.__init__(self, parent, *args)
self.setTabBar(FingerTabBarWidget(self))
This [trivial fingertab gist](https://gist.github.com/LegoStormtroopr/5075267) is released as Public Domain, but boy would it beswell if you could credit me, or tweet me [@LegoStormtoopr](http://www.twitter.com/legostormtroopr) to say thanks!
@matejrazpotnik
Copy link

Hi!
Thank you for awesome piece of code!
One additional concern. This code does not run properly if you include icons in your tabs. The icons are not displayed... just text. Do you know how to implement this as well?
Thanks!

@davidruffner
Copy link

Really nice example!
I also am running into the issue that @jrvidotti raised: when making the tabs closeable the X button shows up in the center of the tab. I went ahead a posted a question to stack overflow about how to position the X button. Thanks again for creating this example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment