2015-11-17 55 views
1

我需要模擬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_()) 

回答

1

爲任何人誰需要一個答案在將來,我已經聘請了專人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://stackoverflow.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_()) 
0

的想法工作,沒有深人地觀察到這一點。我想尋找一種方式來限制你的Unicode設置嚴格ASCII說128 ......

[CHR(x)的有效範圍內(128)X] - >顯示值..

+0

我試圖通過100或更多,但我得到空字符串 你可以通過調用self.sendkeys(100)來試試我的代碼,看看你會得到什麼 –