我使用pyqt5创建了一个教室座位表

时间:2019-12-05 04:35:23

标签: python-3.x pyqt5

到目前为止,我已经掌握了课程座位表的布局和定义的鼠标按下事件。但是,由于在单击图表中的空白点然后输入人员姓名时,图表的整个列都会被填充,而不仅仅是特定的网格点。

import sys, threading, time
from PyQt5.QtGui import QPainter, QColor, QFont, QPen, QBrush
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QDialog, QInputDialog, QErrorMessage

ROWS = 5
SEATS = 3
CELL_SIZE = 90
GRID_ORIGINX = 50
GRID_ORIGINY = 50

class Seating(QWidget):

  def __init__(self): 
    super().__init__()
    self.__desks = [['']*SEATS]*ROWS
    self.setWindowTitle('Seating Chart')
    self.setGeometry(300, 300, 2 * GRID_ORIGINX + CELL_SIZE * SEATS, \
        3 * GRID_ORIGINY + CELL_SIZE * ROWS)
    self.show()


  def paintEvent(self, event): 
    qp = QPainter()
    blackPen = QPen(Qt.black)
    qp.begin(self)
    qp.setPen(blackPen)
    for r in range(ROWS):
      for c in range(SEATS):
        qp.drawRect(GRID_ORIGINX + c * CELL_SIZE, GRID_ORIGINY + r * CELL_SIZE, CELL_SIZE,\ CELL_SIZE)
        qp.setPen(Qt.blue)
        qp.drawText(GRID_ORIGINX + c * CELL_SIZE + 3, GRID_ORIGINY + r * CELL_SIZE + 20,\
            self.__desks[r][c])
        qp.setPen(blackPen)
    qp.end()

  def mousePressEvent(self, event): 
    row = (event.y() - GRID_ORIGINY) // CELL_SIZE
    col = (event.x() - GRID_ORIGINX) // CELL_SIZE
    if 0 <= row < ROWS and 0 <= col < SEATS:
      name,ok = QInputDialog.getText(self, 'Add a guest', 'Enter name:')
      if(len(name)>15):
          QErrorMessage(self).showMessage('Name too long, please try again. Must be less than 15 letters.')
      else:
        self.__desks[row][col] = name
    self.update()



if __name__ == '__main__':
  app = QApplication(sys.argv)
  ex = Seating()
  sys.exit(app.exec_())

0 个答案:

没有答案