gpt4 book ai didi

My GUI still freezes after implementing QThread(实现QThread后,我的GUI仍然冻结)

转载 作者:bug小助手 更新时间:2023-10-22 16:57:01 32 4
gpt4 key购买 nike



So I have been writing a GUI to compress TIF files. libraries used are osgeo gdal and rasterio, aside from PyQt. I followed a tutorial on PyQt Threading here and it initially worked for me, at least I think so.

所以我一直在写一个GUI来压缩TIF文件。除了PyQt之外,使用的库还有osgeo-gdal和rasterio。我在这里学习了一个关于PyQt线程的教程,它最初对我有效,至少我认为是这样。


Later on, I'm at lost at what change I had done that make it doesn't work anymore but my GUI now freezes while performing the compress function which I put inside a worker class.

后来,我不知道我做了什么改变,使它不再工作,但我的GUI现在在执行我放在工人类中的压缩功能时冻结了。


Here is the code for the container of the worker class inside my MainWindow class:

以下是我的MainWindow类中worker类的容器的代码:


def compress_worker(self):

#check if input is valid
if self.error_check():
return None

self.label_5.setStyleSheet("color: black")

self.label_5.setText("Compressing...")

self.thread = QThread()
# Step 3: Create a worker object
self.worker = Worker()
# Step 4: Move worker to the thread
self.worker.moveToThread(self.thread)
# Step 5: Connect signals and slots
self.thread.started.connect(self.worker.run)
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.worker.progress.connect(self.reportProgress)
# Step 6: Start the thread

self.thread.start()

self.pushButton_3.setEnabled(False)
self.pushButton_4.setEnabled(False)
self.thread.finished.connect(
lambda: self.pushButton_3.setEnabled(True)
)
self.thread.finished.connect(
lambda: self.pushButton_4.setEnabled(True)
)
self.thread.finished.connect(
lambda: self.label_5.setText("Finished!")
)

and here is the compress function inside the worker class:

这里是worker类内部的压缩函数:


class Worker(QObject):
finished = pyqtSignal()
progress = pyqtSignal(int)

def run(self):


compression=["JPEG","LZW","DEFLATE"]
output_filename = destfolder + r"\tile_"

#open file using rasterio to get dimensions
raster = rast.open(inputfile)

#determine the number of files generated (divide by 4 means 4 rows, 4 columns, 16 files total)
tile_size_x = int((raster.width/tiles)+(raster.width%tiles != 0))
tile_size_y = int((raster.height/tiles)+(raster.height%tiles != 0))

ds = gdal.Open(inputfile)
band = ds.GetRasterBand(1)
xsize = band.XSize
ysize = band.YSize

iteration = 0

for i in range(0, xsize, tile_size_x):
for j in range(0, ysize, tile_size_y):

#try delete file if it already exists
try:
os.remove(str(output_filename) + str(i) + "_" + str(j) + ".tif")
except OSError:
pass

dest = str(output_filename) + str(i) + "_" + str(j) + ".tif"
options = gdal.TranslateOptions(format="GTIFF",widthPct=compression_level,heightPct=compression_level,creationOptions=["COMPRESS="+compression[compression_index]],srcWin=[i,j,tile_size_x,tile_size_y])
gdal.Translate(dest,inputfile,options=options)
iteration = iteration + 1
self.progress.emit(iteration)

self.finished.emit()

I've tried removing some lines of codes and see if it still freezes and it did. I also made sure not to manipulate any PyQt widgets in my worker thread, but it still freezes the same.

我试着删除了一些代码行,看看它是否仍然冻结,它确实冻结了。我还确保不会在我的工作线程中操作任何PyQt小部件,但它仍然会冻结。


I know questions like this had been asked before but none of the solutions helped me. Would greatly appreciate the help, thanks in advance!

我知道以前有人问过这样的问题,但没有一个解决方案对我有帮助。非常感谢您的帮助,提前谢谢!


更多回答

Does this answer your question? PySide6 QThread still freezing main GUI

这能回答你的问题吗?PySide6 QThread仍在冻结主GUI

Please provide a proper minimal reproducible example.

请提供一个适当的最小可复制示例。

优秀答案推荐
更多回答

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com