【问题标题】:image doesn't change at events图像在事件中不会改变
【发布时间】:2021-12-20 10:10:28
【问题描述】:

我想实现一个图形用户界面,每次按键事件发生时图像都会更新,这是一个尝试,但图像没有更新。

class Test(QMainWindow):

 def __init__(self, path):
     super().__init__()
     self.path = path
     self.sliceno = 40
     self.initUI()


 def vol(self):
     slices = [1,2,3,4,5,6]
     return slices


 def keyPressEvent(self, event):
     key = event.key()
     if event.key() == QtCore.Qt.Key_Q:  #Event
         self.sliceno = self.sliceno + 1

 def initUI(self):
  
     im = np.uint8(self.vol()[self.sliceno]) #should change the index when 'q' pressed
     qimage = QImage(im, im.shape[1], im.shape[0], QImage.Format_Grayscale8)
     pixmap = QPixmap(qimage)
     pixmap_label.setPixmap(pixmap)

     self.setCentralWidget(pixmap_label)
     self.show()

【问题讨论】:

标签: python pyqt


【解决方案1】:

你必须获得新的 QPixmap,所以你必须创建一个更新的函数:

def keyPressEvent(self, event):
    if event.key() == QtCore.Qt.Key_Q:  # Event
        self.sliceno = self.sliceno + 1
        self.change()

def initUI(self):
    self.pixmap_label = QLabel()
    self.change()
    self.setCentralWidget(self.pixmap_label)
    self.show()

def change(self):
    im = np.uint8(self.vol()[self.sliceno])
    qimage = QImage(im, im.shape[1], im.shape[0], QImage.Format_Grayscale8)
    pixmap = QPixmap.fromImage(qimage)
    self.pixmap_label.setPixmap(pixmap)

【讨论】:

  • 如果有第二张图片怎么办?我必须创建第二个函数来更新change2() 以及pixmap2?有没有使用事件更新两个图像的示例,全部使用 QPixmap ?谢谢老兄!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
相关资源
最近更新 更多