CX_FREEZE窗口-AttributeError:“ NoneType”对象没有属性写入 [英] CX_FREEZE Window - AttributeError: 'NoneType' object has no attribute write

查看:74
本文介绍了CX_FREEZE窗口-AttributeError:“ NoneType”对象没有属性写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用cx_freeze 6.1(6.2和6.3有一些不同的问题)来为OpenCV python应用程序生成MSI Windows安装程序。编译成功,并生成了MSI安装程序。当base设置为 None时,
Installer可以正常工作。或控制台。但是,它为base = Win32GUI抛出错误。
我将基础设置为Win32GUI,因为我不希望用户看到提示窗口或不允许他们意外关闭应用程序。
请参阅附件以查看完整的错误。我尝试了其他帖子中提到的一些旧解决方案,但没有任何帮助。我尝试将点击添加到排除项中,但也没有帮助。感谢所有相关的答复。

I am currently using cx_freeze 6.1 (had some different issues with 6.2 and 6.3) to generate MSI windows installer for an OpenCV python application. Compilation was successful and generated MSI installer. Installer works fine when base is set to "None" or "Console". However, it throws error for base = "Win32GUI". I am setting the base to Win32GUI because I don't want the users to see the prompt windows or allow them to accidentally close the application. Refer the attachment to see the complete error. I have tried few old solutions as mentioned in other posts but nothing helped. I tried including 'click' in excludes, it didn't help as well. Appreciate any relevant responses.

Windows GUI中的错误

推荐答案

经过两天的挣扎,我找到了自己的答案。分享,这样可以帮助他人。
base = Win32GUI; -尝试将所有strrr和stdout消息写入Windows GUI。但是,由于内存有限,大多数情况下它将失败,因为它将尝试将这些错误重定向到某些文件。

After struggle of two days, I found my own answer. Sharing so it can help others. base="Win32GUI" - This try writing all strerr and stdout messages to Windows GUI. However, with limited memory, it will fail most of the times as it will try redirecting those errors to some file.

最佳方法如下:

将所有stderr和stdout消息重定向到任何文件,例如日志文件。我按照以下说明进行操作:如何将stdout和stderr重定向到记录器并创建记录器编写器:

Redirect all stderr and stdout messages to any file, e.g. log file. I followed instructions of: How to redirect stdout and stderr to logger in Python and created logger writer:

import sys

class LoggerWriter:
def __init__(self, level):
    # self.level is really like using log.debug(message)
    # at least in my case
    self.level = level

def write(self, message):
    # if statement reduces the amount of newlines that are
    # printed to the logger
    if message != '\n':
        self.level(message)

def flush(self):
    # create a flush method so things can be flushed when
    # the system wants to. Not sure if simply 'printing'
    # sys.stderr is the correct way to do it, but it seemed
    # to work properly for me.
    self.level(sys.stderr)

然后在app.py或主flask文件中创建日志和/或错误文件。下面的示例创建error.log

Then in app.py or your main flask file, create log and/or error file(s). Example below creates error.log

import configparser
import logging
import sys
from LoggerWriter import LoggerWriter
from pathlib import Path
from logging.handlers import TimedRotatingFileHandler
from threading import Timer

# Create Logger if doesn't exist
Path("log").mkdir(parents=True, exist_ok=True)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
handler = TimedRotatingFileHandler('log/error.log', when="midnight", 
interval=1, encoding='utf8')
handler.suffix = "%Y-%m-%d"
handler.setFormatter(formatter)
logger = logging.getLogger()
logger.setLevel(logging.ERROR)
logger.addHandler(handler)
sys.stdout = LoggerWriter(logging.debug)
sys.stderr = LoggerWriter(logging.warning)

if __name__ == '__main__':
   app.run()

这篇关于CX_FREEZE窗口-AttributeError:“ NoneType”对象没有属性写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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