调用添加的窗口时,弹出窗口的编写/关闭代码停止工作,为什么?

时间:2018-12-20 16:04:14

标签: python pyqt pyqt5

第一个示例程序运行正常。当用户单击一个已排列的QLineEdit单元格时,该程序将打开一个NumPad,并且当用户使用鼠标选择数字并按“ Enter”键时,该数字将放置在选定的QLineEdit单元格中,并且NumPad将关闭。但是,在第二个程序中添加了调用屏幕以打开屏幕“ ss”时,“ ss.lVal [(...]和” ss.numpad.close()”行将停止工作。

工作示例代码:

#!/usr/bin/env python

import sys
from pprint import pprint
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import pyqtSignal
from PyQt5 import QtCore, QtGui, QtWidgets

# Global variables
# Data Select - 3=3 Data Rows,4=4 Data Rows,5=5 DataRows
gvDataSel = 3
gvPickX = 0
gvPickY = 0

class extQLineEdit(QLineEdit):
    clicked=pyqtSignal()
    def __init__(self,parent):
        QLineEdit.__init__(self,parent)
    def mousePressEvent(self,QMouseEvent):
        self.clicked.emit()


class wdScoreSheet(QWidget):
    def __init__(self):
        super(wdScoreSheet, self).__init__()

        global gvDataSel

        self.resize(400, 300)

        self.numpad = NumPad(self)

        # Value array
        self.lVal = {}
        for i in range(gvDataSel):
            for j in range(2):
                k = 0
                self.lVal[i,j] = extQLineEdit(self)
                self.lVal[i,j].setGeometry(QtCore.QRect(((j*140)+10),((i*34)+50), 50, 28))
                self.lVal[i,j].clicked.connect(lambda i=i, j=j: self.show_NumPad(i,j))


    def show_NumPad(self, parm1, parm2):
        global gvPickX
        global gvPickY
        gvPickX = parm1
        gvPickY = parm2
        self.numpad.show()

    def close(self):
        self.numpad.close()
        super(wdScoreSheet, self).close()


class NumPad(QWidget):
    def __init__(self, parm1):
        super(NumPad, self).__init__()

        grid_layout = QGridLayout()
        self.setLayout(grid_layout)
        values = [
            '1', '2', '3',
            '4', '5', '6',
            '7', '8', '9',
            '-', '0', 'Enter'
        ]
        positions = [(i, j) for i in range(1,5) for j in range(3)]
        # position is an array of tuples
        for position, value in zip(positions, values):
            if value == '':
                continue
            button = QPushButton(value)
            grid_layout.addWidget(button, *position)
            button.clicked.connect(self.btnclick)

        self.setWindowTitle('Num Pad')

        verticalLayout = QVBoxLayout()
        self.lineEdit = QLineEdit()
        verticalLayout.addWidget(self.lineEdit)
        grid_layout.addLayout(verticalLayout, 0, 0, 1, 3)



    def btnclick(self):
        sender = self.sender()
        #pprint("You Pressed: " + sender.text())
        if sender.text() in ['0','1','2','3','4','5','6','7','8','9']:
            self.lineEdit.setText(self.lineEdit.text() + sender.text())

        if sender.text() in ['-']:
            if self.lineEdit.text()[:1] in ['-']:
                self.lineEdit.setText(self.lineEdit.text()[-(len(self.lineEdit.text())-1):])
            else:
                self.lineEdit.setText(sender.text() + self.lineEdit.text())

        if sender.text() in ["Enter"]:
            if self.lineEdit.text() not in [""]:
                ss.lVal[(gvPickX, gvPickY)].setText(self.lineEdit.text())
            self.lineEdit.clear()
            ss.numpad.close()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    ss = wdScoreSheet()
    ss.show()
    sys.exit(app.exec_())

添加与MainWindow相同的代码无法正常工作

#!/usr/bin/env python

import sys
from pprint import pprint
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import pyqtSignal
from PyQt5 import QtCore, QtGui, QtWidgets

# Global variables
# Data Select - 3=3 Data Rows,4=4 Data Rows,5=5 DataRows
gvDataSel = 3
gvPickX = 0
gvPickY = 0

class extQLineEdit(QLineEdit):
    clicked=pyqtSignal()
    def __init__(self,parent):
        QLineEdit.__init__(self,parent)
    def mousePressEvent(self,QMouseEvent):
        self.clicked.emit()


class wdScoreSheet(QWidget):
    def __init__(self):
        super(wdScoreSheet, self).__init__()

        global gvDataSel

        self.resize(400, 300)

        self.numpad = NumPad(self)

        # Value array
        self.lVal = {}
        for i in range(gvDataSel):
            for j in range(2):
                k = 0
                self.lVal[i,j] = extQLineEdit(self)
                self.lVal[i,j].setGeometry(QtCore.QRect(((j*140)+10),((i*34)+50), 50, 28))
                self.lVal[i,j].clicked.connect(lambda i=i, j=j: self.show_NumPad(i,j))


    def show_NumPad(self, parm1, parm2):
        global gvPickX
        global gvPickY
        gvPickX = parm1
        gvPickY = parm2
        self.numpad.show()

    def close(self):
        self.numpad.close()
        super(wdScoreSheet, self).close()


class NumPad(QWidget):
    def __init__(self, parm1):
        super(NumPad, self).__init__()

        grid_layout = QGridLayout()
        self.setLayout(grid_layout)
        values = [
            '1', '2', '3',
            '4', '5', '6',
            '7', '8', '9',
            '-', '0', 'Enter'
        ]
        positions = [(i, j) for i in range(1,5) for j in range(3)]
        # position is an array of tuples
        for position, value in zip(positions, values):
            if value == '':
                continue
            button = QPushButton(value)
            grid_layout.addWidget(button, *position)
            button.clicked.connect(self.btnclick)

        self.setWindowTitle('Num Pad')

        verticalLayout = QVBoxLayout()
        self.lineEdit = QLineEdit()
        verticalLayout.addWidget(self.lineEdit)
        grid_layout.addLayout(verticalLayout, 0, 0, 1, 3)



    def btnclick(self):
        sender = self.sender()
        #pprint("You Pressed: " + sender.text())
        if sender.text() in ['0','1','2','3','4','5','6','7','8','9']:
            self.lineEdit.setText(self.lineEdit.text() + sender.text())

        if sender.text() in ['-']:
            if self.lineEdit.text()[:1] in ['-']:
                self.lineEdit.setText(self.lineEdit.text()[-(len(self.lineEdit.text())-1):])
            else:
                self.lineEdit.setText(sender.text() + self.lineEdit.text())

        if sender.text() in ["Enter"]:
            if self.lineEdit.text() not in [""]:
                ss.lVal[(gvPickX, gvPickY)].setText(self.lineEdit.text())
            self.lineEdit.clear()
            ss.numpad.close()

class wdMainWindow(QMainWindow):
    def __init__(self):
        super(wdMainWindow, self).__init__()
        self.resize(400, 300)

        self.pbSHR = QPushButton(self)
        self.pbSHR.setText("Open Data Screen")
        self.pbSHR.setGeometry(QtCore.QRect(20, 20, 200, 30))
        self.pbSHR.clicked.connect(self.pbSHR_clicked)

    def pbSHR_clicked(self):
        global gvDataSel
        gvDataSel = 4
        ss = wdScoreSheet()
        ss.show()


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

添加MainWindow来调用ss(wdScoreSheet的打开实例)使“ ss.lVal [(...”和“ ss.numpad.close()”)现在失败,为什么?需要进行哪些更改才能使NumPad正常工作第一个程序吗?谢谢您的帮助。

0 个答案:

没有答案