Pyqt5 視窗縮放和關閉事件

son John
2 min readFeb 2, 2020

--

  • 視窗縮放時會觸發 resizeEvent ,而視窗關閉時會觸發 closeEvent 因此有物件要在視窗縮放或關閉時操作就寫入這些function裡
from PyQt5 import QtWidgets, QtGui, QtCoreclass MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setupUi()
self.show()
def setupUi(self):
self.resize(400, 400)
def resizeEvent(self, event):
print('resize')
width, height = event.size().width(), event.size().height()
print(event.size())
print(width, height)
def closeEvent(self, event):
print('window close')
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())

--

--