Advertisement
egor230

Контекстное меню

Aug 12th, 2024
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | Source Code | 0 0
  1. import subprocess, sys
  2. from PyQt5 import QtWidgets, QtCore
  3. from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QHBoxLayout
  4. from PyQt5.QtCore import Qt
  5. from pynput import mouse
  6.  
  7. clean = f'''#!/bin/bash      
  8. echo -n "" | xclip -i -selection primary
  9. '''
  10. subprocess.run(['bash', '-c', clean])
  11. get_clipboard = f'''#!/bin/bash
  12. xclip -o -selection primary
  13. '''
  14.  
  15. class MouseEventThread(QtCore.QThread):
  16.   mouse_event_signal = QtCore.pyqtSignal(str)
  17.   def __init__(self, parent=None):
  18.     super().__init__(parent)
  19.   def on_click(self, x, y, button, pressed):
  20.     if button == mouse.Button.left and not pressed:
  21.       # Получаем текст из буфера обмена
  22.       current_text = subprocess.run(['bash', '-c', get_clipboard], stdout=subprocess.PIPE, text=True).stdout.strip()
  23.       self.mouse_event_signal.emit(current_text)  # Отправляем сигнал с текстом
  24.  
  25.   def run(self):
  26.     with mouse.Listener(on_click=self.on_click) as listener:
  27.       listener.join()
  28.  
  29. class MyWindow(QtWidgets.QWidget):
  30.   def __init__(self, parent=None):
  31.     super().__init__(parent)
  32.     self.previous_text = ""
  33.     self.mouse_event_thread = MouseEventThread()
  34.     self.mouse_event_thread.mouse_event_signal.connect(self.handle_mouse_event)
  35.     self.mouse_event_thread.start()
  36.  
  37.     self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
  38.     self.setGeometry(1800, 1010, 45, 20)
  39.     layout = QVBoxLayout()
  40.     # Создаем горизонтальный макет для кнопок
  41.     button_layout = QHBoxLayout()
  42.  
  43.     cut_button = QPushButton('Вырезать')
  44.     cut_button.clicked.connect(self.cut_text)
  45.     button_layout.addWidget(cut_button)
  46.  
  47.     copy_button = QPushButton('Копировать')
  48.     copy_button.clicked.connect(self.copy_text)
  49.     button_layout.addWidget(copy_button)
  50.  
  51.     paste_button = QPushButton('Вставить')
  52.     paste_button.clicked.connect(self.paste_text)
  53.     button_layout.addWidget(paste_button)
  54.  
  55.     # Добавляем горизонтальный макет с кнопками в основной вертикальный макет
  56.     layout.addLayout(button_layout)
  57.  
  58.     self.setLayout(layout)
  59.   def handle_mouse_event(self, current_text):
  60.  
  61.     if current_text and current_text != self.previous_text:
  62.       self.previous_text = current_text
  63.       self.show()
  64.       subprocess.run(['bash', '-c', clean])
  65.     else:
  66.       self.hide()
  67.  
  68.   def cut_text(self):
  69.     self.hide()
  70.     script = f'''#!/bin/bash
  71.      xte "keydown Control_R"  "key x" "keyup Control_R"  '''
  72.     subprocess.run(['bash', '-c', script])
  73.  
  74.   def copy_text(self):
  75.     self.hide()
  76.     script = '''#!/bin/bash
  77.          xte "keydown Control_R" "key c" "keyup Control_R" '''
  78.     subprocess.run(['bash', '-c', script])
  79.  
  80.   def paste_text(self):
  81.     self.hide()
  82.     script = '''#!/bin/bash
  83.        xte "keydown Control_R" "key v" "keyup Control_R" '''
  84.     subprocess.run(['bash', '-c', script])
  85.  
  86. app = QtWidgets.QApplication(sys.argv)
  87. window = MyWindow()  # Запуск потока
  88. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement