PyQt5窗口关闭问题

时间:2018-12-20 19:13:13

标签: python pyqt5

不确定我是否正确地提出了这个要求,但是我已经浏览了所有可用资源,但仍不清楚,因此无法使下面的第二个程序作为第一个程序起作用。下面的第一个程序通过允许用户单击QLineEdit单元,弹出NumPad并输入数字来工作。当按下“ Enter”键时,数字被写入,并且数字键盘关闭。第二个程序是相同的,除了在主调用窗口中添加了按钮启动窗口“ ss”。 “ ss.numpad.close()”和“ ss.lVal ...”导致第二个程序失败。

工作程序示例:

#!/usr/bin/env python

import sys
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

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__()
        self.numpad = NumPad(self)
        self.lVal = {}
        for i in range(3):
            for j in range(2):
                k = 0
                self.lVal[i,j] = extQLineEdit(self)
                self.lVal[i,j].setGeometry(QtCore.QRect(((j*70)+10),((i*34)+30), 50, 28))
                self.lVal[i,j].clicked.connect(lambda i=i, j=j: self.show_NumPad(i,j))

def show_NumPad(self, parm1, 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)]
        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()
        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 ["Enter"]:
            if self.lineEdit.text() not in [""]:
                ss.lVal[1,1].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_())

程序的主窗口不起作用:

#!/usr/bin/env python

import sys
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

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__()
        self.numpad = NumPad(self)
        self.lVal = {}
        for i in range(3):
            for j in range(2):
                k = 0
                self.lVal[i,j] = extQLineEdit(self)
                self.lVal[i,j].setGeometry(QtCore.QRect(((j*70)+10),((i*34)+30), 50, 28))
                self.lVal[i,j].clicked.connect(lambda i=i, j=j: self.show_NumPad(i,j))

def show_NumPad(self, parm1, 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)]
        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()
        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 ["Enter"]:
            if self.lineEdit.text() not in [""]:
                ss.lVal[1,1].setText(self.lineEdit.text())
            self.lineEdit.clear()
            ss.numpad.close()

class wdMainWindow(QMainWindow):
    def __init__(self):
        super(wdMainWindow, self).__init__()
        self.pbSHR = QPushButton(self)
        self.pbSHR.setText("Open Data Screen")
        self.pbSHR.setGeometry(QtCore.QRect(10, 10, 160, 30))
        self.pbSHR.clicked.connect(self.pbSHR_clicked)

    def pbSHR_clicked(self):
        ss = wdScoreSheet()
        ss.show()

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

为什么“ ss.numpad.close()”在第二个程序中关闭弹出窗口时使用错误的句柄,但在第一个示例中有效?谢谢你的帮助。

0 个答案:

没有答案