setText()实际上不适用于Python

时间:2012-06-09 19:07:57

标签: python input label pyqt4 settext

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

1 个答案:

答案 0 :(得分:1)

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)