我需要模擬PyQt4中或pyside,我已經有了下面的代碼這是很好的按鍵發送擊鍵,但它增加了唯一的資本拉丁字母,不小的或其他語言的字母被允許從unicode字符串的PyQt 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://stackoverflow.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_())
我試圖通過100或更多,但我得到空字符串 你可以通過調用self.sendkeys(100)來試試我的代碼,看看你會得到什麼 –