Guest User

embed_terminal_1.py

a guest
Jun 29th, 2020
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. from qtpy.QtWidgets import QTabWidget, QPushButton
  2. from qtpy import QtWidgets, QtGui, QtCore
  3. #import gi
  4. #from gi.repository import Wnck, Gdk
  5. import time
  6. import sys
  7.  
  8. class Container(QtWidgets.QTabWidget):
  9.     def __init__(self):
  10.         QtWidgets.QTabWidget.__init__(self)
  11.         self.setDocumentMode(True)
  12.         self.setTabPosition(QTabWidget.South)
  13.         self._new_button = QPushButton(self)
  14.         self._new_button.setText("New SSH Session")
  15.         self.setCornerWidget(self._new_button)
  16.         self.setTabsClosable(True)
  17.         self.setMovable(True)
  18.         self.embed('xterm')
  19.  
  20.     def embed(self, command, *args):
  21.         proc = QtCore.QProcess()
  22.         proc.setProgram(command)
  23.         proc.setArguments(args)
  24.         #started, procId = proc.startDetached()
  25.         #pid = None
  26.         #started = proc.startDetached(pid)
  27.         # https://stackoverflow.com/q/31519215 : "overload" startDetached : give three arguments, get a tuple(boolean,PID)
  28.         # NB: we will get a failure `xterm: No absolute path found for shell: .` even if we give it an empty string as second argument; must be a proper abs path to a shell
  29.         started, procId = proc.startDetached(command, ["/bin/bash"], ".")
  30.         if not started:
  31.             QtWidgets.QMessageBox.critical(self, 'Command "{}" not started!'.format(command), "Eh")
  32.             return
  33.  
  34.         # attempts = 0
  35.         # while attempts < 10:
  36.         #     screen = Wnck.Screen.get_default()
  37.         #     screen.force_update()
  38.         #     # do a bit of sleep, else window is not really found
  39.         #     time.sleep(0.1)
  40.         #     # this is required to ensure that newly mapped window get listed.
  41.         #     while Gdk.events_pending():
  42.         #         Gdk.event_get()
  43.         #     for w in screen.get_windows():
  44.         #         print(attempts, w.get_pid(), procId, w.get_pid() == procId)
  45.         #         if w.get_pid() == procId:
  46.         #             self.window = QtGui.QWindow.fromWinId(w.get_xid())
  47.         #             #container = QtWidgets.QWidget.createWindowContainer(window, self)
  48.         #             proc.setParent(self)
  49.         #             #self.scrollarea = QtWidgets.QScrollArea()
  50.         #             #self.container = QtWidgets.QWidget.createWindowContainer(self.window)
  51.         #             # via https://vimsky.com/zh-tw/examples/detail/python-method-PyQt5.QtCore.QProcess.html
  52.         #             #pid = proc.pid()
  53.         #             #win32w = QtGui.QWindow.fromWinId(pid) # nope, broken window
  54.         #             win32w = QtGui.QWindow.fromWinId(w.get_xid()) # this finally works
  55.         #             win32w.setFlags(QtCore.Qt.FramelessWindowHint)
  56.         #             widg = QtWidgets.QWidget.createWindowContainer(win32w)
  57.         #
  58.         #             #self.container.layout = QtWidgets.QVBoxLayout(self)
  59.         #             #self.addTab(self.container, command)
  60.         #             self.addTab(widg, command)
  61.         #             #self.scrollarea.setWidget(self.container)
  62.         #             #self.container.setParent(self.scrollarea)
  63.         #             #self.scrollarea.setWidgetResizable(True)
  64.         #             #self.scrollarea.setFixedHeight(400)
  65.         #             #self.addTab(self.scrollarea, command)
  66.         #             self.resize(500, 400) # set initial size of window
  67.         #             return
  68.         #     attempts += 1
  69.         QtWidgets.QMessageBox.critical(self, 'Window not found', 'Process started but window not found')
  70.  
  71.  
  72. app = QtWidgets.QApplication(sys.argv)
  73. w = Container()
  74. w.show()
  75. sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment