从 unicode 字符串 pyqt pyside 发送击键
send keystrokes from unicode string pyqt pyside
我需要在 pyqt4 或 pyside 中模拟击键,我已经有了下面的代码,它很好,但它只添加大写拉丁字母,不允许使用小写字母或其他语言字母
def sendkeys(self, char, modifier=QtCore.Qt.NoModifier):
event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier)
QtCore.QCoreApplication.postEvent(self, event)
def sendstring(self, string):
for chara in string:
print(chara)
self.sendkeys(char=QtGui.QKeySequence.fromString(str(chara))[0])
这是一个完整的例子,可以让这个想法与拉丁字母一起工作
from PySide import QtCore, QtGui, QtWebKit
class WebPage(QtWebKit.QWebPage):
"""
QWebPage that prints Javascript errors.
"""
def javaScriptConsoleMessage(self, message, lineNumber, sourceID):
print('Javascript error at line number %d' % lineNumber)
print('%s' % message)
print('Source ID: %s' % sourceID)
class myWindow(QtWebKit.QWebView):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.loadFinished.connect(self.on_loadFinished)
self.runloadfinished = True
self.web_page = WebPage()
self.setPage(self.web_page)
self.load(QtCore.QUrl('http://whosebug.com/'))
def on_loadFinished(self):
if self.runloadfinished:
self.sendkeys(QtCore.Qt.Key_Tab)
self.sendstring('Hello World')
self.sendkeys(QtCore.Qt.Key_Enter)
self.sendkeys(QtCore.Qt.Key_Return)
self.runloadfinished = False
def sendkeys(self, char, modifier=QtCore.Qt.NoModifier):
event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier)
QtCore.QCoreApplication.postEvent(self, event)
def sendstring(self, string):
for chara in string:
print(chara)
self.sendkeys(char=QtGui.QKeySequence.fromString(str(chara))[0])
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myWindow')
main = myWindow()
main.show()
sys.exit(app.exec_())
没有深入研究这个。我会寻找一种方法将您的 Unicode 集限制为严格的 ASCII,比如 128...
[chr(x) for x in range(128)] --> 显示值..
对于将来需要答案的任何人,我在 upwork.com 上雇了人并得到了答案,这是一个完整的工作示例,它适用于 python 2 和 3、pyqt 和 pyside
# -*- coding: utf-8 -*-
from PySide import QtCore, QtGui, QtWebKit
class WebPage(QtWebKit.QWebPage):
"""
QWebPage that prints Javascript errors.
"""
def javaScriptConsoleMessage(self, message, lineNumber, sourceID):
print('Javascript error at line number %d' % lineNumber)
print('%s' % message)
print('Source ID: %s' % sourceID)
class myWindow(QtWebKit.QWebView):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.loadFinished.connect(self.on_loadFinished)
self.runloadfinished = True
self.web_page = WebPage()
self.setPage(self.web_page)
self.load(QtCore.QUrl('http://whosebug.com/'))
def on_loadFinished(self):
if self.runloadfinished:
self.sendkeys(QtCore.Qt.Key_Tab)
self.sendstring("لا اله الا الله ")
#self.sendstring("Hello World")
self.sendkeys(QtCore.Qt.Key_Enter)
self.sendkeys(QtCore.Qt.Key_Return)
self.runloadfinished = False
def sendkeys(self, char, modifier=QtCore.Qt.NoModifier, text=None):
if not text:
event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier)
else:
event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier, text)
QtCore.QCoreApplication.postEvent(self, event)
def sendstring(self, string):
for chara in string:
print(chara)
# QtCore.QString() is missing when using PyQt4 for Python3
# since str() is unicode by default on Python3
# http://inputvalidation.blogspot.in/2010/10/python3-pyqt4-and-missing-qstring.html
try:
text = QtCore.QString(chara)
except AttributeError:
text = str(chara)
self.sendkeys(char=QtGui.QKeySequence.fromString(str(chara))[0],
text=text)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myWindow')
main = myWindow()
main.show()
sys.exit(app.exec_())
我需要在 pyqt4 或 pyside 中模拟击键,我已经有了下面的代码,它很好,但它只添加大写拉丁字母,不允许使用小写字母或其他语言字母
def sendkeys(self, char, modifier=QtCore.Qt.NoModifier):
event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier)
QtCore.QCoreApplication.postEvent(self, event)
def sendstring(self, string):
for chara in string:
print(chara)
self.sendkeys(char=QtGui.QKeySequence.fromString(str(chara))[0])
这是一个完整的例子,可以让这个想法与拉丁字母一起工作
from PySide import QtCore, QtGui, QtWebKit
class WebPage(QtWebKit.QWebPage):
"""
QWebPage that prints Javascript errors.
"""
def javaScriptConsoleMessage(self, message, lineNumber, sourceID):
print('Javascript error at line number %d' % lineNumber)
print('%s' % message)
print('Source ID: %s' % sourceID)
class myWindow(QtWebKit.QWebView):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.loadFinished.connect(self.on_loadFinished)
self.runloadfinished = True
self.web_page = WebPage()
self.setPage(self.web_page)
self.load(QtCore.QUrl('http://whosebug.com/'))
def on_loadFinished(self):
if self.runloadfinished:
self.sendkeys(QtCore.Qt.Key_Tab)
self.sendstring('Hello World')
self.sendkeys(QtCore.Qt.Key_Enter)
self.sendkeys(QtCore.Qt.Key_Return)
self.runloadfinished = False
def sendkeys(self, char, modifier=QtCore.Qt.NoModifier):
event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier)
QtCore.QCoreApplication.postEvent(self, event)
def sendstring(self, string):
for chara in string:
print(chara)
self.sendkeys(char=QtGui.QKeySequence.fromString(str(chara))[0])
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myWindow')
main = myWindow()
main.show()
sys.exit(app.exec_())
没有深入研究这个。我会寻找一种方法将您的 Unicode 集限制为严格的 ASCII,比如 128...
[chr(x) for x in range(128)] --> 显示值..
对于将来需要答案的任何人,我在 upwork.com 上雇了人并得到了答案,这是一个完整的工作示例,它适用于 python 2 和 3、pyqt 和 pyside
# -*- coding: utf-8 -*-
from PySide import QtCore, QtGui, QtWebKit
class WebPage(QtWebKit.QWebPage):
"""
QWebPage that prints Javascript errors.
"""
def javaScriptConsoleMessage(self, message, lineNumber, sourceID):
print('Javascript error at line number %d' % lineNumber)
print('%s' % message)
print('Source ID: %s' % sourceID)
class myWindow(QtWebKit.QWebView):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.loadFinished.connect(self.on_loadFinished)
self.runloadfinished = True
self.web_page = WebPage()
self.setPage(self.web_page)
self.load(QtCore.QUrl('http://whosebug.com/'))
def on_loadFinished(self):
if self.runloadfinished:
self.sendkeys(QtCore.Qt.Key_Tab)
self.sendstring("لا اله الا الله ")
#self.sendstring("Hello World")
self.sendkeys(QtCore.Qt.Key_Enter)
self.sendkeys(QtCore.Qt.Key_Return)
self.runloadfinished = False
def sendkeys(self, char, modifier=QtCore.Qt.NoModifier, text=None):
if not text:
event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier)
else:
event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char, modifier, text)
QtCore.QCoreApplication.postEvent(self, event)
def sendstring(self, string):
for chara in string:
print(chara)
# QtCore.QString() is missing when using PyQt4 for Python3
# since str() is unicode by default on Python3
# http://inputvalidation.blogspot.in/2010/10/python3-pyqt4-and-missing-qstring.html
try:
text = QtCore.QString(chara)
except AttributeError:
text = str(chara)
self.sendkeys(char=QtGui.QKeySequence.fromString(str(chara))[0],
text=text)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myWindow')
main = myWindow()
main.show()
sys.exit(app.exec_())