cx_freeze问题与Mac上的相对路径 [英] cx_freeze issue with relative path on mac

查看:39
本文介绍了cx_freeze问题与Mac上的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac上遇到cx_freeze相对路径逻辑问题.我正在使用python 3.3和pyqt5并构建可移植的应用程序Windows-Mac.

I'm having issues with cx_freeze relative path logic on mac. I'm using python 3.3 and pyqt5 and building an app portable windows - mac.

以下简单脚本仅将图像作为QPixmap加载到QLabel中.

The following simple script just loads an image into a QLabel as a QPixmap.

从控制台启动时,该代码在Windows和Mac上均能正常运行,并且在Windows和Mac上均可以构建.该exe文件可以在Windows上正常运行.该应用程序(MacOSX)无法加载图像,与dmg相同.

The code works fine both on windows and mac when launched from console, builds both on windows and mac. The exe works on windows without any problem. The app ( MacOSX ) does not load the image, same for the dmg.

我觉得这与相对路径设置有关.这种信念的原因如下:-如果我尝试加载.app,图片将不会显示-如果将testimage.jpeg复制粘贴到.app所在的文件夹中,则会加载该图像.

I have a feeling it has something to do with relative path setting. The reason for this belief is the following: - if I try to load the .app the image will not appear - if I copy-paste the testimage.jpeg in the same folder where the .app is it will load the image.

我确实尝试了安装程序中的include_files,但没有结果.

I did try include_files in the setup with no results.

任何人都可以帮忙吗?

以下是脚本:首先是文件image_insert_test.py:

Here are the scripts: First the file image_insert_test.py:

import sys
from PyQt5.QtWidgets import (QApplication, QDialog,
        QErrorMessage, QLabel,  QWidget, QVBoxLayout )

from PyQt5.QtCore import pyqtSlot, QDir, Qt
from PyQt5 import QtGui



class Window(QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)



        self.fill_box2 = QLabel()
        pixmap = QtGui.QPixmap('testimage.jpeg')
        self.fill_box2.setPixmap(pixmap.scaled(100,100,Qt.KeepAspectRatio))


        # set the layout
        layout = QVBoxLayout()
        layout.addWidget(self.fill_box2)
        self.setLayout(layout)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Window()
    main.setWindowTitle('test gui')
    main.show()
    sys.exit(app.exec_())

文件setup.py:

the file setup.py:

import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'


options = {
    'build_exe': {
        "includes": ["matplotlib.backends.backend_macosx"] ,
        "include_files" : ["testimage.jpeg"]
    }
}

executables = [
    Executable('image_insert_test.py', base=base)
]

setup(name='image_insert_test',
      version='0.1',
      description='Sample image_insert_test script',
      options=options,
      executables=executables
      )

这是cx_freeze bdist_mac的日志 http://pastebin.com/77uU4Exr

and this is the log of the cx_freeze bdist_mac http://pastebin.com/77uU4Exr

推荐答案

以下是一种解决方法:代替

a workaround is the following: instead of

pixmap = QtGui.QPixmap('testimage.jpeg')

地点:

pixmap = QtGui.QPixmap(os.path.join(os.getcwd(),'image_insert_test-0.1.app/Contents/MacOS','testimage.jpeg')

这会从.app包中获取图像路径,但是必须有一种更为优雅的方法来实现它.

this fishes the image path from the .app package but there has to be a more elegant way to do it.

这篇关于cx_freeze问题与Mac上的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆