import sys
from PyQt4 import QtCore,QtGui
from Windows.intro_win import Ui_Introduction
from Windows.post_adder_win import Ui_postTool
import win32clipboard
import pyHook

class Intro(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui = Ui_Introduction()
        self.ui.setupUi(self)
class postTool(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui2 = Ui_postTool()
        self.ui2.setupUi(self)
    def setModPosition(self):
        self.ui2.lineEdit_2.setText("something")
        print "nomnom"


def OnKeyboardEvent(event):
    KeyClicked = event.KeyID
    if KeyClicked == 121:
        print "Debug from keyboardEvent"
        OMD_post.show()
        postTool().setModPosition()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    OMD_intro = Intro()
    OMD_post = postTool()
    OMD_intro.show()
    eventkey = pyHook.HookManager()
    eventkey.KeyDown = OnKeyboardEvent
    eventkey.HookKeyboard()
    sys.exit(app.exec_())


它实际上不起作用-文本没有更改-为什么不起作用?它不会给我任何错误(但是打印会打印出应该显示的内容)
https://dl.dropbox.com/u/21125662/compilation/_00398.jpg

最佳答案

postTool().setModPosition()中的OnKeyboardEvent更改了新创建的postTool的lineEdit中的文本,因为您没有show()新的小部件,所以您不会看到它。您可能想做的是在setText上调用OMD_post

尝试用OnKeyBoardEvent中的keyPressEvent函数替换postTool模块级别的函数,或者也许使用QShortCut。

class postTool(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui2 = Ui_postTool()
        self.ui2.setupUi(self)
    def setModPosition(self):
        self.ui2.lineEdit_2.setText("something")
        print "nomnom"
    def keyPressEvent(event):
        if type(event) == QtGui.QKeyEvent:
            key = str(event.text()).lower()
            if key == 'whatever':
                self.setModPosition()


或者,

class postTool(QtGui.QMainWindow):
    def __init__(self,parent=None):
        ... # the existing stuff
        QtGui.QShortcut(QtGui.QKeySequence("F10"), self, self.setModPosition)

09-12 17:57