如何在QTextEdit框PYQT中显示虚拟键盘上按下的数字

时间:2015-09-14 09:07:41

标签: python python-2.7 pyqt pyqt4

如何在QTextEdit框中显示虚拟键盘上按下的数字? (PyQt的)

#!/usr/bin/python
""" Prototype code for the infusion pump
code has to be updated """

""" Imports """
import sys
from PyQt4 import QtGui, QtCore
import sys, time
from PyQt4.QtCore import QSize, SIGNAL

""" Globals """
global topTxt, midTxt, endTxt
global powerOpt, AlrmSil, bootC
global btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9
global btn0,btnDot,btnBK,btnClr,btnMain,btnChg,btnOpt,btnClrTot,btnPWR,btnAlrm,btnStrt,btnStop
global highVal, lowVal

highVal = 55
lowVal = 10
""" Globals Initialization"""
powerOpt = 0
AlrmSil = 0
class Infusion(QtGui.QMainWindow):

    def __init__(self):
        """ Constructor """
        super(Infusion, self).__init__()        
        self.initKey()
        self.initText()
        self.showUI()


    def initKey(self):   
        """   This function initializes the buttons in the UI """ 
        global btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9
        global btn0,btnDot,btnBK,btnClr,btnMain,btnChg,btnOpt,btnClrTot,btnPWR,btnAlrm,btnStrt,btnStop
        btn1 = QtGui.QPushButton("1", self)
        btn1.setFixedWidth(30)
        btn1.move(650, 50)
        btn1.setEnabled(False)

        btn2 = QtGui.QPushButton("2", self)
        btn2.setFixedWidth(30)
        btn2.move(690, 50)
        btn2.setEnabled(False)

        btn3 = QtGui.QPushButton("3", self)
        btn3.setFixedWidth(30)
        btn3.move(730, 50)

        btn4 = QtGui.QPushButton("4", self)
        btn4.setFixedWidth(30)
        btn4.move(650, 100)

        btn5 = QtGui.QPushButton("5", self)
        btn5.setFixedWidth(30)
        btn5.move(690, 100)

        btn6 = QtGui.QPushButton("6", self)
        btn6.setFixedWidth(30)
        btn6.move(730, 100)

        btn7 = QtGui.QPushButton("7", self)
        btn7.setFixedWidth(30)
        btn7.move(650, 150)

        btn8 = QtGui.QPushButton("8", self)
        btn8.setFixedWidth(30)
        btn8.move(690, 150)

        btn9 = QtGui.QPushButton("9", self)
        btn9.setFixedWidth(30)
        btn9.move(730, 150)

        btnBK = QtGui.QPushButton("Back", self)
        btnBK.setFixedWidth(40)
        btnBK.move(650, 200)

        btn0 = QtGui.QPushButton("0", self)
        btn0.setFixedWidth(30)
        btn0.move(690, 200)

        btnDot = QtGui.QPushButton(".", self)
        btnDot.setFixedWidth(30)
        btnDot.move(730, 200)

        btnMain = QtGui.QPushButton("", self)
        btnMain.setFixedWidth(70)
        btnMain.move(60, 230)


        btn3.clicked.connect(self.digitClicked)            
        btn4.clicked.connect(self.digitClicked)
        btn5.clicked.connect(self.digitClicked)            
        btn6.clicked.connect(self.digitClicked)
        btn7.clicked.connect(self.digitClicked)            
        btn8.clicked.connect(self.digitClicked)
        btn9.clicked.connect(self.digitClicked)            
        btn0.clicked.connect(self.digitClicked)

        btnDot.clicked.connect(self.digitClicked)
        btnBK.clicked.connect(self.digitClicked)
        # btnClr.clicked.connect(self.digitClicked)

        btnMain.clicked.connect(self.mainMenuClk)


        self.update()
    def initText(self):
        """ This portion initializes the Display of the UI, which is in black and green color """
        global topTxt, midTxt, endTxt


        midTxt = QtGui.QTextEdit("", self)
        midTxt.setFixedWidth(550)
        midTxt.setFixedHeight(100)
        midTxt.setReadOnly(True)
        midTxt.setStyleSheet('QTextEdit {background-color: #00FF00; color: black;}')
        midTxt.move(50, 80)
        midTxt.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)



    def showUI(self):
        """ everything initialized , now just show it """
        self.statusBar()        
        self.setGeometry(300, 300, 800, 300)
        self.setWindowTitle('InfusionUI')
        self.show()



    def digitClicked(self):
        sender = self.sender()
        self.emit(SIGNAL("sigKeyButtonClicked"), sender.text())
        print sender.text()







    def alignSecScreen(self,scrn, numScreen):
        global topTxt, midTxt, endTxt, btn1
        if numScreen == 2:
            midCsr = scrn.textCursor()
            midCsr.movePosition(QtGui.QTextCursor.End)
            self.midTxtInsert( 4 * ("\t"),8, "")

        elif numScreen == 3:
            midCsr = scrn.textCursor()
            midCsr.movePosition(QtGui.QTextCursor.End)
            self.midTxtInsert( 3 * ("\t"),8, "")

        elif numScreen == 4:            
            midCsr = scrn.textCursor()
            midCsr.movePosition(QtGui.QTextCursor.End)
            self.midTxtInsert( 2 * ("\t"),8, "")
        else:
            pass

    def mainMenuClk(self):
        """ Option 1 selection handler """
        global topTxt, midTxt, endTxt, btn1
        # topTxt.clear()
        midTxt.clear()
        # endTxt.clear()
        selection = 1
        self.midTxtInsert("\nMax:", 10, "")
        self.alignSecScreen(midTxt,2)
        self.midTxtInsert("59\n", 10, "")

        self.midTxtInsert("High:", 10, "")
        self.alignSecScreen(midTxt,2)
        self.midTxtInsert("%s\n"%highVal, 10, "")

        self.midTxtInsert("Low:\n", 10, "")  

        self.midTxtInsert("Min:\n", 10, "")  

        if(selection):
            sender = self.sender()
            selectionTxt = QtGui.QTextEdit("", self)
            selectionTxt.setFixedWidth(50)
            selectionTxt.setFixedHeight(30)
            # selectionTxt.setReadOnly(True)
            selectionTxt.setStyleSheet('QTextEdit {background-color: #000000; color: green;}')
            selectionTxt.move(500, 125)
            selectionTxt.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
            selectionTxt.show()
            selectionTxt.setAlignment(QtCore.Qt.AlignCenter)


            cursor = selectionTxt.textCursor()
            textSelected = cursor.selectedText()
            # s = textSelected.upper()
            print textSelected
            selectionTxt.setText(textSelected)


            selectionTxt.update()
            self.update()



    def midTxtInsert(self, txt,font, algmt):
        """ UI is split to three region, this is mid part which is in green """
        global topTxt, midTxt, endTxt        
        midTxt.setFontPointSize(font)        
        if algmt.upper() == "C":
            midTxt.setAlignment(QtCore.Qt.AlignCenter)
        elif algmt.upper() == "R":
            midTxt.setAlignment(QtCore.Qt.AlignRight)
        else:
            midTxt.setAlignment(QtCore.Qt.AlignLeft)  
        midTxt.insertPlainText(txt)
        self.update()         




def main():
    """ Function main """
    app = QtGui.QApplication(sys.argv)
    ex = Infusion()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

在这里添加一个片段,因为我无法分享实际代码,我正在尝试的是,

点击左按钮后 - >将获得一个黑色文本编辑框。当我按下数字时,我试图将输入的数字输入到框中。

1 个答案:

答案 0 :(得分:1)

I modified your code as follows:

#!/usr/bin/python
""" Prototype code for the infusion pump
code has to be updated """

""" Imports """
import sys
from PyQt4 import QtGui, QtCore
import sys, time
from PyQt4.QtCore import QSize, SIGNAL, pyqtSignal

""" Globals """
global topTxt, midTxt, endTxt
global powerOpt, AlrmSil, bootC
global btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9
global btn0,btnDot,btnBK,btnClr,btnMain,btnChg,btnOpt,btnClrTot,btnPWR,btnAlrm,btnStrt,btnStop
global highVal, lowVal

highVal = 55
lowVal = 10
""" Globals Initialization"""
powerOpt = 0
AlrmSil = 0
class Infusion(QtGui.QMainWindow):
    sigTextButtonClicked = pyqtSignal(QtCore.QString)

    def __init__(self):
        """ Constructor """
        super(Infusion, self).__init__()
        self.initKey()
        self.initText()
        self.showUI()


    def initKey(self):
        """   This function initializes the buttons in the UI """
        global btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9
        global btn0,btnDot,btnBK,btnClr,btnMain,btnChg,btnOpt,btnClrTot,btnPWR,btnAlrm,btnStrt,btnStop
        btn1 = QtGui.QPushButton("1", self)
        btn1.setFixedWidth(30)
        btn1.move(650, 50)
        btn1.setEnabled(False)

        btn2 = QtGui.QPushButton("2", self)
        btn2.setFixedWidth(30)
        btn2.move(690, 50)
        btn2.setEnabled(False)

        btn3 = QtGui.QPushButton("3", self)
        btn3.setFixedWidth(30)
        btn3.move(730, 50)

        btn4 = QtGui.QPushButton("4", self)
        btn4.setFixedWidth(30)
        btn4.move(650, 100)

        btn5 = QtGui.QPushButton("5", self)
        btn5.setFixedWidth(30)
        btn5.move(690, 100)

        btn6 = QtGui.QPushButton("6", self)
        btn6.setFixedWidth(30)
        btn6.move(730, 100)

        btn7 = QtGui.QPushButton("7", self)
        btn7.setFixedWidth(30)
        btn7.move(650, 150)

        btn8 = QtGui.QPushButton("8", self)
        btn8.setFixedWidth(30)
        btn8.move(690, 150)

        btn9 = QtGui.QPushButton("9", self)
        btn9.setFixedWidth(30)
        btn9.move(730, 150)

        btnBK = QtGui.QPushButton("Back", self)
        btnBK.setFixedWidth(40)
        btnBK.move(650, 200)

        btn0 = QtGui.QPushButton("0", self)
        btn0.setFixedWidth(30)
        btn0.move(690, 200)

        btnDot = QtGui.QPushButton(".", self)
        btnDot.setFixedWidth(30)
        btnDot.move(730, 200)

        btnMain = QtGui.QPushButton("", self)
        btnMain.setFixedWidth(70)
        btnMain.move(60, 230)


        btn3.clicked.connect(self.digitClicked)
        btn4.clicked.connect(self.digitClicked)
        btn5.clicked.connect(self.digitClicked)
        btn6.clicked.connect(self.digitClicked)
        btn7.clicked.connect(self.digitClicked)
        btn8.clicked.connect(self.digitClicked)
        btn9.clicked.connect(self.digitClicked)
        btn0.clicked.connect(self.digitClicked)

        btnDot.clicked.connect(self.digitClicked)
        btnBK.clicked.connect(self.digitClicked)
        # btnClr.clicked.connect(self.digitClicked)

        btnMain.clicked.connect(self.mainMenuClk)


        self.update()
    def initText(self):
        """ This portion initializes the Display of the UI, which is in black and green color """
        global topTxt, midTxt, endTxt


        midTxt = QtGui.QTextEdit("", self)
        midTxt.setFixedWidth(550)
        midTxt.setFixedHeight(100)
        midTxt.setReadOnly(True)
        midTxt.setStyleSheet('QTextEdit {background-color: #00FF00; color: black;}')
        midTxt.move(50, 80)
        midTxt.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)



    def showUI(self):
        """ everything initialized , now just show it """
        self.statusBar()
        self.setGeometry(300, 300, 800, 300)
        self.setWindowTitle('InfusionUI')
        self.show()



    def digitClicked(self):
        sender = self.sender()
        self.sigTextButtonClicked.emit(sender.text())
        print sender.text()







    def alignSecScreen(self,scrn, numScreen):
        global topTxt, midTxt, endTxt, btn1
        if numScreen == 2:
            midCsr = scrn.textCursor()
            midCsr.movePosition(QtGui.QTextCursor.End)
            self.midTxtInsert( 4 * ("\t"),8, "")

        elif numScreen == 3:
            midCsr = scrn.textCursor()
            midCsr.movePosition(QtGui.QTextCursor.End)
            self.midTxtInsert( 3 * ("\t"),8, "")

        elif numScreen == 4:
            midCsr = scrn.textCursor()
            midCsr.movePosition(QtGui.QTextCursor.End)
            self.midTxtInsert( 2 * ("\t"),8, "")
        else:
            pass

    def mainMenuClk(self):
        """ Option 1 selection handler """
        global topTxt, midTxt, endTxt, btn1
        # topTxt.clear()
        midTxt.clear()
        # endTxt.clear()
        selection = 1
        self.midTxtInsert("\nMax:", 10, "")
        self.alignSecScreen(midTxt,2)
        self.midTxtInsert("59\n", 10, "")

        self.midTxtInsert("High:", 10, "")
        self.alignSecScreen(midTxt,2)
        self.midTxtInsert("%s\n"%highVal, 10, "")

        self.midTxtInsert("Low:\n", 10, "")

        self.midTxtInsert("Min:\n", 10, "")

        if(selection):
            sender = self.sender()
            selectionTxt = QtGui.QTextEdit("", self)
            selectionTxt.setFixedWidth(50)
            selectionTxt.setFixedHeight(30)
            # selectionTxt.setReadOnly(True)
            selectionTxt.setStyleSheet('QTextEdit {background-color: #000000; color: green;}')
            selectionTxt.move(500, 125)
            selectionTxt.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
            selectionTxt.show()
            selectionTxt.setAlignment(QtCore.Qt.AlignCenter)


            cursor = selectionTxt.textCursor()
            textSelected = cursor.selectedText()
            # s = textSelected.upper()
            print textSelected
            selectionTxt.setText(textSelected)

            self.sigTextButtonClicked.connect(selectionTxt.setText)
            selectionTxt.update()
            self.update()



    def midTxtInsert(self, txt,font, algmt):
        """ UI is split to three region, this is mid part which is in green """
        global topTxt, midTxt, endTxt
        midTxt.setFontPointSize(font)
        if algmt.upper() == "C":
            midTxt.setAlignment(QtCore.Qt.AlignCenter)
        elif algmt.upper() == "R":
            midTxt.setAlignment(QtCore.Qt.AlignRight)
        else:
            midTxt.setAlignment(QtCore.Qt.AlignLeft)
        midTxt.insertPlainText(txt)
        self.update()




def main():
    """ Function main """
    app = QtGui.QApplication(sys.argv)
    ex = Infusion()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

and it works.

First, I define the signal sigTextButtonClicked = pyqtSignal(QtCore.QString), then connect it to slot setText (you may want to define your own slot for to, for example, obtain multi-digit numbers in text field): self.sigTextButtonClicked.connect(selectionTxt.setText)

And I emit this signal every time digit was clicked:

def digitClicked(self):
    sender = self.sender()
    self.sigTextButtonClicked.emit(sender.text())
    print sender.text()

And take a look here, the syntax is pretty clear: http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html