Advertisement
furas

Python - PyQt - Close Tab with Ctrl+W

Aug 19th, 2016
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4. from PyQt5.QtCore import *
  5. from PyQt5.QtGui import *
  6. from PyQt5.QtWidgets import *
  7.  
  8. class Window(QWidget):
  9.    
  10.     def __init__(self):
  11.         super().__init__(self)
  12.  
  13.         self.shortcut = QShortcut(QKeySequence("Ctrl+W"), self, self.on_close_tab)
  14.  
  15.         self.tabs = QTabWidget(self)
  16.         self.tabs.resize(600, 300)
  17.  
  18.         self.show()
  19.  
  20.         # add few tabs
  21.         for i in range(1, 11):
  22.             e = QTextEdit(self.tabs)
  23.             e.append("Hello World number {} !".format(i))
  24.             self.tabs.addTab(e, "#{}".format(i))
  25.  
  26.     @pyqtSlot()
  27.     def on_close_tab(self):
  28.         print("DEBUG: tabs count:", self.tabs.count())
  29.  
  30.         if self.tabs.count():
  31.             print("DEBUG: tabs current index:", self.tabs.currentIndex())
  32.             self.tabs.removeTab(self.tabs.currentIndex())
  33.  
  34. app = QApplication(sys.argv)
  35. win = Window()
  36. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement