SoFunction
Updated on 2025-03-05

PythonQT5 package exe thread use

Pack:

pyinstaller --noconsole --onefile 

–noconsole means that the command line is not required to be opened

Revise:

Resource files that are needed in general projects, such as lib, png, exe, etc.
Need to modify the spec file separately

  pathex=['.'],
  binaries=[
    ('D:/', '.'),
    ('D:/', '.'),
     ('D:/', '.')
    ],

Then execute the following command to package and load the resource file into the exe.

pyinstaller 

Just use the relative path in the py code.

Methods to get the image path in py:

def resource_path(relative_path):
    """Absolute path to obtain resources"""
    try:
        # Temporary folder created by PyInstaller, or the path when packaged in --onefile mode        base_path = sys._MEIPASS
    except Exception:
        # If it is not a packaged environment, use the current working directory        base_path = (".")
    rp = (base_path, relative_path)
    return rp

use:

 font_path = resource_path("") 
 print('font_path: ', font_path)
 font_size = 16
 font = (font_path, font_size)

Hidden and realistic QT elements:

    def on_hide_show_click(self, params):
        if self._is_show == 1:
            self.frame_2.hide()
            self.frame_3.hide()
            # Remove limits minimum height and width            (0, 0)
            ((), 50)
            self._is_show = 0
        else:
            self.frame_2.show()
            self.frame_3.show()
            ((), ())
            # ()
            self._is_show = 1

The button click method brings parameters:

 (lambda: self.on_hide_show_click(1))

Thread usage:

# define thread classclass ThreadDemoHandle(QThread):
    # Define signals to send data from thread to main thread    data_received = pyqtSignal(list)

	# p1 is a parameter, there are multiple parameters.    def __init__(self, p1):
        super().__init__()
        self.p1 = p1

    def run(self):
        try:
            # Processing logic, such as sending requests, etc.  Computational complex logic.  .  .            # Send a signal to the main thread            self.data_received.emit([self.p1, 'result'])
        except Exception as e:
        	# flush=True will force the log to the file. If it is not added, sometimes the file will not be written.            print('Thread processing error:', e, flush=True)

# use = ThreadDemoHandle(1200)
The # handle_result_data method is to process the result returned by the thread.data_received.connect(self.handle_result_data)
()


@pyqtSlot(list)
def handle_result_data(self, result):
	print('result:', result[0], result[1])
	# For example, rendering Interface and so on。

This is the article about the use of PythonQT5 packaged exe threads. For more related content on PythonQT5 packaged exe threads, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!