list化すれば容易にできた。
import sys from PyQt5 import QtCore from PyQt5.QtWidgets import * from PyQt5.QtGui import QImage, QPixmap ql_x_position = (200, 400, 600, 200, 400, 600, 200, 400, 600) qr_x_position = (1160, 1360, 1560, 1160, 1360, 1560, 1160, 1360, 1560) q_y_position = (80, 80, 80, 280, 280, 280, 480, 480, 480) a_x_position = (25, 215, 405, 595, 785, 975, 1165, 1355, 1545, 1735) a_y_position = (700, 890) class Window(QWidget): def __init__(self): super().__init__() self.initUI() self.question = True def initUI(self): self.label_list_left = [QLabel(self) for i in range(9)] self.label_list_right = [QLabel(self) for i in range(9)] for i in range(9): self.label_list_left[i].setGeometry(QtCore.QRect(ql_x_position[i], q_y_position[i], 160, 160)) for i in range(9): self.label_list_right[i].setGeometry(QtCore.QRect(qr_x_position[i], q_y_position[i], 160, 160)) self.label_list_2 = [QLabel(self) for i in range(10)] self.label_list_3 = [QLabel(self) for i in range(10)] for i in range(10): self.label_list_2[i].setGeometry(QtCore.QRect(a_x_position[i], a_y_position[0], 160, 160)) for i in range(10): self.label_list_3[i].setGeometry(QtCore.QRect(a_x_position[i], a_y_position[1], 160, 160)) def keyPressEvent(self, e): if e.key() == QtCore.Qt.Key_0: if self.question == True: # 画像の表示 for i in range(9): self.label_list_left[i].setPixmap(QPixmap.fromImage(image)) self.label_list_right[i].setPixmap(QPixmap.fromImage(image)) for i in range(10): self.label_list_2[i].setPixmap(QPixmap.fromImage(image)) self.label_list_3[i].setPixmap(QPixmap.fromImage(image)) self.question = False else: # 画像の消去 for i in range(9): self.label_list_left[i].clear() self.label_list_right[i].clear() for i in range(10): self.label_list_2[i].clear() self.label_list_3[i].clear() self.question = True # エスケープキーを押すと画面が閉じる if e.key() == QtCore.Qt.Key_Escape: self.close() image = QImage('donut.png').scaled(160,160,QtCore.Qt.KeepAspectRatio) app = QApplication(sys.argv) ex =Window() ex.showFullScreen() sys.exit(app.exec_())