问题描述:

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_())

It doesn't really work - text didn't change - why it doesn't work? It doesn't give me any error (but print prints what it should)

https://dl.dropbox.com/u/21125662/compilation/_00398.jpg

网友答案:

postTool().setModPosition() in OnKeyboardEvent changes the text in the lineEdit of a newly created postTool, which you won't see since you don't show() the new widget. What you probably want to do is to call setText on OMD_post.

Try replacing the OnKeyBoardEvent module level function with a keyPressEvent function in the postTool, or maybe use a 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()

Alternatively,

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