gpt4 book ai didi

python - 如何使用 Pyqt 小部件创建键盘和鼠标事件

转载 作者:太空宇宙 更新时间:2023-11-03 15:07:21 25 4
gpt4 key购买 nike

我刚刚开始在 Pycharms 中使用 python 3.x 进行 Qt 设计。我看过一个将 .ui 文件转换为 .py 文件的教程。我现在很好。我正在实现一个聊天程序。

现在,我想输入 TextEdit、linEdit,但它不等我输入。我想将键盘或鼠标事件与它们连接起来,这样当我单击时,它会等待我输入,当我按 Enter 时,它会存储字符串。

空格和#here 之间的代码就是我需要帮助的地方。基本上我想为这些事件定义方法并稍后从它们调用它们。

我的Ui_MainWindow类如下:

class Ui_MainWindow(QtGui.QMainWindow):

def __init__(self):
super(Ui_MainWindow,self).__init__()

self.setObjectName(_fromUtf8("MainWindow"))
self.resize(611, 487)
self.setTabShape(QtGui.QTabWidget.Rounded)
self.centralwidget = QtGui.QWidget(self)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.frame = QtGui.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(10, 30, 591, 41))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.label = QtGui.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 10, 81, 17))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(320, 10, 66, 17))
self.label_2.setObjectName(_fromUtf8("label_2"))
#Here
self.lineEdit = QtGui.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(90, 10, 221, 21))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
#Here
self.lineEdit_3 = QtGui.QLineEdit(self.frame)
self.lineEdit_3.setGeometry(QtCore.QRect(360, 10, 221, 21))
self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))

self.frame_2 = QtGui.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(10, 70, 291, 361))
self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtGui.QFrame.Raised)
self.frame_2.setObjectName(_fromUtf8("frame_2"))
#Here
self.textEdit = QtGui.QTextEdit(self.frame_2)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 271, 301))
self.textEdit.setObjectName(_fromUtf8("textEdit"))

self.pushButton_3 = QtGui.QPushButton(self.frame_2)
self.pushButton_3.setGeometry(QtCore.QRect(10, 310, 161, 41))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.pushButton_4 = QtGui.QPushButton(self.frame_2)
self.pushButton_4.setGeometry(QtCore.QRect(180, 310, 98, 41))
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.pushButton_4.clicked.connect(self.clrLogs) # Clear Logs from WidgetList by clicking
self.verticalScrollBar = QtGui.QScrollBar(self.frame_2)
self.verticalScrollBar.setGeometry(QtCore.QRect(260, 10, 20, 301))
self.verticalScrollBar.setMinimumSize(QtCore.QSize(16, 301))
self.verticalScrollBar.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor))
self.verticalScrollBar.setAutoFillBackground(False)
self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
self.verticalScrollBar.setInvertedAppearance(False)
self.verticalScrollBar.setObjectName(_fromUtf8("verticalScrollBar"))

######################################################
# Scroll to the bottom of chat windows
# self.textEdit.verticalScrollBar().setValue(self.textEdit.verticalScrollBar().maximum)

self.frame_3 = QtGui.QFrame(self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(300, 70, 301, 361))
self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtGui.QFrame.Raised)
self.frame_3.setObjectName(_fromUtf8("frame_3"))
self.listWidget = QtGui.QListWidget(self.frame_3)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 281, 341))
self.listWidget.setObjectName(_fromUtf8("listWidget"))
self.verticalScrollBar_2 = QtGui.QScrollBar(self.frame_3)
self.verticalScrollBar_2.setGeometry(QtCore.QRect(270, 10, 21, 341))
self.verticalScrollBar_2.setOrientation(QtCore.Qt.Vertical)
self.verticalScrollBar_2.setObjectName(_fromUtf8("verticalScrollBar_2"))
self.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(self)
self.menubar.setGeometry(QtCore.QRect(0, 0, 611, 25))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuMenu_Actions = QtGui.QMenu(self.menubar)
self.menuMenu_Actions.setObjectName(_fromUtf8("menuMenu_Actions"))
self.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(self)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
self.setStatusBar(self.statusbar)
self.actionVersion = QtGui.QAction(self)
self.actionVersion.setObjectName(_fromUtf8("actionVersion"))
self.actionVersion.triggered.connect(Chat.app_ver) # When submenu Action item Version is clicked
self.actionExit = QtGui.QAction(self)
self.actionExit.setObjectName(_fromUtf8("actionExit"))
self.actionExit.triggered.connect(qApp.quit) #When submenu Action item Exit is clicked
self.menuMenu_Actions.addAction(self.actionVersion)
self.menuMenu_Actions.addAction(self.actionExit)
self.menubar.addAction(self.menuMenu_Actions.menuAction())
self.retranslateUi(self)
QtCore.QMetaObject.connectSlotsByName(self)

def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", WindowTitle, None))
self.setWindowIcon(QtGui.QIcon('chat_icon'))
self.label.setText(_translate("MainWindow", "IP Address:", None))
self.label_2.setText(_translate("MainWindow", "Nick:", None))
self.pushButton_3.setText(_translate("MainWindow", "Send SMS", None))
self.pushButton_4.setText(_translate("MainWindow", "Clear Logs", None))
self.menuMenu_Actions.setTitle(_translate("MainWindow", "Menu Actions", None))
self.actionVersion.setText(_translate("MainWindow", "Version", None))
self.actionExit.setText(_translate("MainWindow", "Exit", None))

def clrLogs(self):
self.listWidget.clear()

enter image description here最左边的是 TextEdit,最右边的是 listWidget。我想输入 TextEdit 并存储它,然后将此消息发送到 Chatlog(ListWidget)。

最佳答案

我会开始说你必须关心以下事情:

  1. 您的键盘 UI,比如说一个小型原型(prototype)。
  2. 注意谁获得了您的键盘焦点。
  3. 注意您的过滤器以捕获您需要的事件。
  4. 通过键盘向声明者对象发送正确的响应。

我会在这里向您展示一些非常好的例子,我花了相当多的时间来做,另一个例子非常简单和小。由于代码量相当大,我将保留此 small example还有这个more complex示例来自 my github .

您可以克隆或复制并粘贴代码,只要确保复制和粘贴时它带有正确的导入即可。

已编辑[已添加]

这是一个小示例,从您传递的接收器捕获事件,并发布等效事件:

import sys

from PyQt5.QtCore import QCoreApplication
from PyQt5.QtCore import QEvent
from PyQt5.QtCore import Qt
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QKeyEvent
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QWidget

class MainWindow(QMainWindow):

def __init__(self):
super(MainWindow, self).__init__()
self.central_widget = QWidget()
self.cw_layout = QHBoxLayout()
self.central_widget.setLayout(self.cw_layout)
self.setCentralWidget(self.central_widget)

self.line = LineEdit()
self.kb = KeyBoard(self.line)

self.cw_layout.addWidget(self.line)

self.create_connections()

def create_connections(self):
self.line.signal_evoke_kb.connect(self.show_kb)

def show_kb(self):
if self.kb.isHidden():
self.kb.show()
else:
self.kb.hide()


class LineEdit(QLineEdit):

signal_evoke_kb = pyqtSignal()

def __init__(self):
super(LineEdit, self).__init__()

def mousePressEvent(self, QMouseEvent):
super(LineEdit, self).mousePressEvent(QMouseEvent)
self.signal_evoke_kb.emit()

class Key(QPushButton):

def __init__(self, name, event, receiver):
super(Key, self).__init__()
self.name = name
self.event = event
self.setText(name)


class KeyBoard(QWidget):

def __init__(self, receiver):
super(KeyBoard, self).__init__()
self.receiver = receiver
self.layout = QHBoxLayout()
self.keys = ['q','w','e','r','t','y']
self.dict_keys ={'q':Qt.Key_Q,'w':Qt.Key_W,'e':Qt.Key_E,'r':Qt.Key_R,'t':Qt.Key_T,'y':Qt.Key_Y,}
for key in self.keys:
key_keyboard = Key(key,self.dict_keys[key],receiver)
key_keyboard.clicked.connect(self.key_pressed)
self.layout.addWidget(key_keyboard)
self.setLayout(self.layout)

def key_pressed(self):
try:
event = QKeyEvent(QEvent.KeyPress, self.sender().event, Qt.NoModifier,
self.sender().name, False)
QCoreApplication.postEvent(self.receiver, event)
except Exception as e:
print(e)

def keyPressEvent(self, evt):
event = QKeyEvent(QEvent.KeyPress, evt.key(), evt.modifiers(),
evt.text(), False)
QCoreApplication.postEvent(self.receiver, event)
evt.ignore()

if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())

通过这种方式,您可以个性化您的 key ,因为您有自己的 key ,并且当您需要大写和其他所有内容时可以更好地组织它。如果您愿意这样做,我强烈建议您查看我之前链接的更完整的示例。

关于python - 如何使用 Pyqt 小部件创建键盘和鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44535561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com