如何使用pyqt4

时间:2018-11-10 15:39:22

标签: python pyqt pyqt4

这是我的示例代码。在我的实际项目中,我在不同位置使用了2行编辑。通过使用此代码,我需要创建一个虚拟键盘。如果我们单击行编辑,则它应该显示一个虚拟键盘。通过使用相同的虚拟键盘,我希望在行编辑中输入我的文本。最后,我创建了一个虚拟键盘,但是问题是它仅适用于第一行编辑,但我也想将同一键盘调用到另一行编辑。我没有得到用于第二行编辑的键盘,任何人都可以告诉我如何显示键盘进行第二行编辑

import sys
from pyface.qt import QtGui, QtCore
from functools import partial

class MainWindow(QtGui.QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.central_widget = QtGui.QWidget()
        self.cw_layout = QtGui.QHBoxLayout()
        self.central_widget.setLayout(self.cw_layout)
        self.setCentralWidget(self.central_widget)
        self.btn = QtGui.QPushButton("save")
        self.btn.clicked.connect(self.savebtn)
        self.line = LineEdit()
        self.line_edit1 = LineEdit()

        self.kb = KeyBoard()


        self.cw_layout.addWidget(self.line)
        self.cw_layout.addWidget(self.btn)

        self.create_connections()

    def savebtn(self):
        global fileName
        self.d1 = QtGui.QDialog()
        self.form1 = QtGui.QFormLayout(self.d1)

        self.name_file = QtGui.QLabel("File Name:")

        self.line_edit1 = LineEdit()

        self.d1.close()
        self.saveBtn = QtGui.QPushButton("Save")
        self.saveBtn.clicked.connect(self.savefile)


        self.canclebtn = QtGui.QPushButton("Cancel")
        self.canclebtn.clicked.connect(self.d1.close)

        self.form1.addRow(self.name_file, self.line_edit1)
        self.form1.addRow( self.canclebtn,self.saveBtn)


        self.d1.setWindowTitle("Enter Grids")
        self.d1.setGeometry(350,200,300,100)
        self.d1.setWindowModality(QtCore.Qt.ApplicationModal)
        self.d1.exec_()
    def savefile(self):
        self.d1.close()

        text = self.line_edit1.text()

        print "line edit text issssssssssss",text

    def create_connections(self):
        self.line.signal_evoke_kb.connect(self.show_kb)
        self.line_edit1.signal_evoke_kb.connect(self.show_kb)

    def show_kb(self):
        if self.kb.isHidden():

            self.kb.show()
        else:
            self.kb.hide()

class LineEdit(QtGui.QLineEdit):

    signal_evoke_kb = QtCore.Signal()

    def __init__(self):
        super(LineEdit, self).__init__()

    def mousePressEvent(self, QMouseEvent):
        super(LineEdit, self).mousePressEvent(QMouseEvent)
        self.signal_evoke_kb.emit()


class KeyBoard(QtGui.QWidget):

    def __init__(self):
        super(KeyBoard, self).__init__()

        self.vBox = QtGui.QVBoxLayout()
        self.flayout = QtGui.QHBoxLayout()
        self.slayout = QtGui.QHBoxLayout()
        self.tlayout = QtGui.QHBoxLayout()
        self.folayout = QtGui.QHBoxLayout()
        self.btnlayout = QtGui.QHBoxLayout()
        custom_signal = QtCore.Signal()

        for i in range(0,10):
            self.accBtn = QtGui.QPushButton(str(i))
            self.accBtn.clicked.connect(partial(self.keyBoardClicked,i))
            self.flayout.addWidget(self.accBtn)
        for key in [  'q','w','e','r','t','y','u','i','o','p']:
            self.accBtn = QtGui.QPushButton(str(key))
            self.accBtn.clicked.connect(partial(self.keyBoardClicked,key))
            self.slayout.addWidget(self.accBtn)

        for key in [ 'a','s','d','f','g','h','i','j','k','l']:
            self.accBtn = QtGui.QPushButton(str(key))
            self.accBtn.clicked.connect(partial(self.keyBoardClicked,key))
            self.tlayout.addWidget(self.accBtn)
        for key in [ 'z','x','c','v','b','n','m','.' , ',']:
            self.accBtn = QtGui.QPushButton(str(key))
            self.accBtn.clicked.connect(partial(self.keyBoardClicked,key))
            self.folayout.addWidget(self.accBtn)

        self.btn = QtGui.QPushButton("Backspace")
        self.btn.clicked.connect(self.backspace)
        self.btnlayout.addWidget(self.btn)

        self.btn1 = QtGui.QPushButton("Enter")
        self.btn1.clicked.connect(self.enter)

        self.btnlayout.addWidget(self.btn1)

        self.btn2 = QtGui.QPushButton("Close")
        self.btn2.clicked.connect(self.close)

        self.btnlayout.addWidget(self.btn2)


        self.vBox.addLayout(self.flayout)

        self.vBox.addLayout(self.slayout)

        self.vBox.addLayout(self.tlayout)
        self.vBox.addLayout(self.folayout)
        self.vBox.addLayout(self.btnlayout)

        self.setLayout(self.vBox)
        self.setWindowTitle('Virtual keyboard')

    def backspace(self):
        self.line.backspace()


    def keyBoardClicked(self,key):
        pass
    def enter(self):
        print "enterrrrr"



if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

0 个答案:

没有答案