2016-07-22 60 views
0

我使用pyQt和pyInstaller构建一个小程序。 我已尝试将背景图片添加到我的QMainWindow:pyinstaller“无法解析样式表”

class pyPrimaMainWindow(QMainWindow): 
    def __init__(self): 
     ...do some stuff... 
     self.setWindowIcon(QIcon(os.path.join(self.py_prima.resource_path(), "TH.ico"))) # <- this works 
     self.setStyleSheet("QMainWindow{{border-image: url({0});background-size:100%;}}".format(os.path.join(self.py_prima.resource_path(), "bg.png"))) 

的resource_path()方法看起来像这样:

def resource_path(self): 
    """ Get absolute path to resource, works for dev and for PyInstaller """ 
    # PyInstaller creates a temp folder and stores path in _MEIPASS 
    base_path = getattr(sys, '_MEIPASS', "C:/Users/Tobias/eclipse/workspace/PyPrima/data/") 
    #   except Exception: 
    #    base_path = 

    print(base_path) 
    return base_path 

它从pyinstaller维基复制,返回absoulte路径和作品为其他图片/图标。 但是,如果我用pyInstaller构建可执行文件,程序运行良好,但背景图像丢失。相反,控制台输出

"could not parse stylesheet of object ..." 

如果我跑python的文件,它工作一切正常...

在这个任何想法?

谢谢!

回答

0

我会回答我自己的问题,以防其他人在同一个问题上绊倒。

的fileseparators是错误的... 与

bgpath = os.path.join(self.py_prima.resource_path(), "bg.png") 
bgpath = bgpath.replace("\\", "/") 
self.setStyleSheet("QMainWindow{{border-image: url({0});background-size: 100%;}}".format(
     bgpath)) 
对其进行修复