marcelotduarte / cx_Freeze

cx_Freeze creates standalone executables from Python scripts, with the same performance, is cross-platform and should work on any platform that Python itself works on.
https://marcelotduarte.github.io/cx_Freeze/
Other
1.28k stars 210 forks source link

division by zero since generated main.exe can't get QPixmap height #2274

Closed snowuyl closed 4 months ago

snowuyl commented 4 months ago

Describe the bug I use cx_Freeze to convert exe file for QPixmap test python program. It can generated exe file but failed to run with "ZeroDivisionError: division by zero" error messages.

To Reproduce

  1. Unzipping QPixmap.zip to D:\ folder.
  2. cd D:\QPixmap
  3. python3 setup.py build
  4. cd D:\QPixmap\build\exe.win-amd64-3.11
  5. main.exe

The following error messages shown on Dos Prompt. D:\QPixmap\build\exe.win-amd64-3.11>main.exe Traceback (most recent call last): File "C:\Users\broth\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\cx_Freeze\initscripts__startup.py", line 124, in run module_init.run(name + "main") File "C:\Users\broth\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\cx_Freeze\initscripts\console.py", line 16, in run exec(code, module_main.dict) File "main.py", line 32, in File "main.py", line 12, in init__ File "main.py", line 25, in initUI ZeroDivisionError: division by zero

Expected behavior cx_Freeze can convert and run exe file for QPixmap test python program successfully.

Desktop (please complete the following information):

snowuyl commented 4 months ago

QPixmap.zip

marcelotduarte commented 4 months ago

Your setup.py is a bit confuse, mixing setuptools options.... But, I belied that you can fix the error using base: executables=[Executable("main.py", base="Win32GUI")],

snowuyl commented 4 months ago

Thanks for your reply! I have changed setup.py as you suggested. But division by zero issue still occurred.

snowuyl commented 4 months ago
division by zero
snowuyl commented 4 months ago

setup.zip

marcelotduarte commented 4 months ago

Another error is that you are not copying the png file (https://cx-freeze.readthedocs.io/en/stable/faq.html#using-data-files) It works with this:

setup.py

from cx_Freeze import setup, Executable

include_files = [("image/PNG_transparency_demonstration_1.png", "image/PNG_transparency_demonstration_1.png")]
setup(
    name="qpixmaptest",
    version="1.0.0", 
    description="qpixmaptest Python package",
    executables=[Executable("main.py",  base="Win32GUI")],
    options={"build_exe": {"include_files": include_files}},
)

main.py

import os
import sys
from PyQt6.QtWidgets  import (QApplication, QWidget, QVBoxLayout, QLabel)
from PyQt6.QtGui      import QPixmap

def find_image(filename):
    if getattr(sys, "frozen", False):  # The application is frozen
        return os.path.join(os.path.dirname(sys.executable), "image", filename)
    return os.path.join(os.path.dirname(__file__), "image", filename)

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('my window')
        self.setGeometry(50, 50, 200, 150)

        layout = QVBoxLayout()
        self.setLayout(layout)

        self.mylabel = QLabel('this is an image', self)
        layout.addWidget(self.mylabel)
        file_path = find_image("PNG_transparency_demonstration_1.png")
        self.mypixmap = QPixmap(file_path)
        aspect_ratio = self.mypixmap.width() / self.mypixmap.height()
        width = 200
        scaled_pixmap = self.mypixmap.scaled(width, int(width / aspect_ratio))
        self.mylabel.setPixmap(scaled_pixmap)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec())
snowuyl commented 4 months ago

Thanks for your great support!