2017-05-02 4 views
0

スペースキーを押したときにウィジェットを設定しようとしています。 私はQLabelを含むQGridLayoutを持っています。 特定のQlabelのテキストをQGridLayoutのcoordから設定したいのですが、私のkeypress関数からQGridLayoutにアクセスすると、その変数はnullになります。PYQT5のkeypressイベントからウィジェットを設定

import sys 
from PyQt5 import QtCore, QtWidgets 
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget 
from PyQt5.QtCore import QSize  

class Position: 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

class HelloWindow(QMainWindow): 
    global gridLayout 
    def __init__(self): 
     QMainWindow.__init__(self) 

     self.setMinimumSize(QSize(640, 640))  
     self.setWindowTitle("Hello world") 

     centralWidget = QWidget(self)   
     self.setCentralWidget(centralWidget) 

     gridLayout = QGridLayout(self) 
     gridLayout.setSpacing(0) 
     centralWidget.setLayout(gridLayout) 

     file_text = open("logic/file.txt", "r") 
     file = file_text.read() 
     file = file.replace("[(","") 
     file = file.replace(")]","") 
     file = file.replace("),(",";") 
     positions = file.split(';') 
     lista = [] 
     for pos in positions: 
     coord = pos.split(',') 
     temp = Position(int(coord[0]),int(coord[1])) 
     lista.append(temp) 
     file_text.close() 

     cont = 0 
     for x in range(0, 9): 
     for y in range(0, 9): 
      if cont < 64: 
       title = QLabel(str(cont+1), self) 
       title.setAlignment(QtCore.Qt.AlignCenter) 
       title.setContentsMargins(0,0,0,0) 
       title.setStyleSheet('QLabel {background-color: white; color: red; font-size: 24px; font-weight: bold}') 
       if cont%2 == 0: 
        title.setStyleSheet('QLabel {background-color: black; color: red; font-size: 24px; font-weight: bold}') 
       gridLayout.addWidget(title,lista[cont].x,lista[cont].y) 
       cont += 1 

    def keyPressEvent(self, event): 
     if event.key() == QtCore.Qt.Key_Escape: 
     self.close() 
     if event.key() == QtCore.Qt.Key_Space: 
     item = gridLayout.itemAtPosition(1,1) 
     w = item.widget() 
     w.setText("test") 

if __name__ == "__main__": 
    app = QtWidgets.QApplication(sys.argv) 
    mainWin = HelloWindow() 
    mainWin.show() 
    sys.exit(app.exec_()) 

次のセクションでは、私が設定してテキストのためのあなたのCOORDからウィジェットにアクセスし、テスト

def keyPressEvent(self, event): 
     if event.key() == QtCore.Qt.Key_Escape: 
     self.close() 
     if event.key() == QtCore.Qt.Key_Space: 
     item = gridLayout.itemAtPosition(1,1) 
     w = item.widget() 
     w.setText("test") 

です。問題は、グローバル変数の誤用から生じる

答えて

0

、これはあなたのケースでは、あなたがそれを使用するたびに宣言する必要があります。

class HelloWindow(QMainWindow): 
    def __init__(self): 
     QMainWindow.__init__(self) 
     [...] 
     global gridLayout 
     gridLayout = QGridLayout(self) 
     [...] 

    def keyPressEvent(self, event): 
     if event.key() == QtCore.Qt.Key_Escape: 
     self.close() 
     if event.key() == QtCore.Qt.Key_Space: 
     global gridLayout 
     item = gridLayout.itemAtPosition(1,1) 
     w = item.widget() 
     w.setText("test") 

よりエレガントな解決策は、の属性として​​を宣言することですクラスは、このために、あなたはすべてのケースでself.gridLayoutに​​を変更する必要があります。

class Position: 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

class HelloWindow(QMainWindow): 
    def __init__(self): 
     QMainWindow.__init__(self) 

     self.setMinimumSize(QSize(640, 640))  
     self.setWindowTitle("Hello world") 

     centralWidget = QWidget(self)   
     self.setCentralWidget(centralWidget) 
     self.gridLayout =QGridLayout(self) 
     self.gridLayout.setSpacing(0) 
     centralWidget.setLayout(self.gridLayout) 

     file_text = open("logic/file.txt", "r") 
     file = file_text.read() 
     file = file.replace("[(","") 
     file = file.replace(")]","") 
     file = file.replace("),(",";") 
     positions = file.split(';') 
     lista = [] 
     for pos in positions: 
      coord = pos.split(',') 
      temp = Position(int(coord[0]),int(coord[1])) 
      lista.append(temp) 
     file_text.close() 

     cont = 0 
     for x in range(0, 9): 
      for y in range(0, 9): 
       if cont < 64: 
        title = QLabel(str(cont+1), self) 
        title.setAlignment(QtCore.Qt.AlignCenter) 
        title.setContentsMargins(0,0,0,0) 
        title.setStyleSheet('QLabel {background-color: white; color: red; font-size: 24px; font-weight: bold}') 
        if cont%2 == 0: 
         title.setStyleSheet('QLabel {background-color: black; color: red; font-size: 24px; font-weight: bold}') 
        self.gridLayout.addWidget(title,lista[cont].x,lista[cont].y) 
        cont += 1 

    def keyPressEvent(self, event): 
     if event.key() == QtCore.Qt.Key_Escape: 
      self.close() 
     if event.key() == QtCore.Qt.Key_Space: 
      item = selself.gridLayout.itemAtPosition(1,1) 
      w = item.widget() 
      w.setText("test") 

if __name__ == "__main__": 
    app = QtWidgets.QApplication(sys.argv) 
    mainWin = HelloWindow() 
    mainWin.show() 
    sys.exit(appself..exec_()) 
+0

それは今作品です、ありがとうございます。 – Joseph

関連する問題