![subDirPath subDirPath]()
本文介绍了从第二个脚本调用时,Python脚本崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个生成PDF报告的报告生成脚本(让我们称之为脚本A)。我想在一个新脚本(脚本B)中导入它,并从这个新脚本中调用这些函数。 问题: 1.脚本A在创建时完全从脚本B调用时崩溃一个QPixmap对象。 Python控制台重新启动。 2.脚本A在单独运行时运行正常,但如果我尝试从命令提示符调用它python.exe崩溃。 我将在下一节中解释并显示代码,因为我设法做的最好的就是找出问题。 我尝试了什么: 以下是脚本B.它所做的只是它导入脚本A,创建一个在A中定义的类的实例,并使用它来调用该对象的方法。 import 脚本A as et a = sample mdbPath = C:\\Python27 \\ test.mdb directory = C:\\Python27 ui = et.HawkAutomat ionScript() ui.Main(mdbPath,directory,a) 脚本A包含一个名为 HawkAutomationScript 。该对象包含两个要注意的函数:一个Main函数和一个生成报告的函数。这些是导入的模块: 来自 random import * import sys import os import subprocess import time 来自 datetime import datetime 来自 PyQt4 import QtCore,QtGui 来自 PyQt4.QtCore import QRect 来自 PyQt4.QtGui import QTextDocument,QPrinter,QApplication,QPainter 来自字符串 import 模板 import shutil 导入时间 import pyodbc 这是类和Main函数的定义: doc = QTextDocument() myCursor = QtGui.QTextCursor(doc) class HawkAutomationScript(object): def Main(self,mdb,directory,name): Ui_FormObject = HawkAutomationScript() # #填写报告## Ui_FormObject.fnGenerateReport(目录,名称) def fnGenerateReport(self,directory,name): now = datetime.now() dateString = now。 strftime( %d-%b-%Y)# 获取当前日期 dirPath = C:\\ \\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ string> SelfTest_Results testName = name + .pdf subDirPath = dirPath + name if (os.path.isdir(dirPath) ): if (os.path.isdir(subDirPath)): subDirPath = subDirPath + testName else : os.mkdir(subDirPath) subDirPath = subDirPath + testName else : os.m kdir(dirPath) os.mkdir(subDirPath) subDirPath = subDirPath + testName pixmap = QtGui.QPixmap(doc.size()。width(),doc.size()。height ())# ############### printer = QPrinter() printer.setOutputFileName(subDirPath) printer.setOutputFormat(QPrinter.PdfFormat) doc.print_(printer) # 创建QPainter以绘制我们的内容 painter = QPainter(pixmap) painter.begin(打印机) doc.drawContents(画家) painter.end() 这是所有相关代码。 如果我运行脚本B,A会在代码中标记的QPixmap行崩溃。但是,如果我从B复制行并自行运行A,则会生成PDF报告。但是如果我自己从命令提示符运行A python.exe崩溃。 任何帮助都会非常感激。谢谢!! 解决方案 以下代码对我来说不对: dirPath = C:\\Users \\michael \\Downloads \\Ethernet \\scripts \\\ ResultName = SelfTest_Results testName = name + 。pdf subDirPath = dirPath # 现在与dirPath相同 如果 (os.path.isdir(dirPath)): if (os.path.isdir(subDirPath)):# 如果两个路径(实际上只有一个)存在 subDirPath = subDirPath + testName # 追加testName,但见下文 else ://这绝不会发生 os.mkdir(subDirPath) subDirPath = subDirPath + testName else : os.mkdir(dirPath)# dirPath不存在,所以创建它 os.mkdir(subDirPath)# 现在再次创建它? subDirPath = subDirPath + testName # 现在追加testName,但为时已晚。 [edit] 应该是什么: dirPath = C:\\Users \\michael \\Downloads \\\\\\\\\\\\\\\ pan> ResultName = SelfTest_Results testName = name + 。pdf subDirPath = dirPath + name # 追加名称,testName是PDF文件的名称 如果 (!os.path.isdir(dirPath)): os.mkdir(dirPath)# dirPath没有存在,所以创建它 如果(!os.path.isdir(subDirPath)): # subDirPath不存在,所以创建它 os.mkdir(subDirPath) ... [/ edit] I have a report generation script (let's call this script A)that generates a PDF report. I want to import this in a new script (script B) and call the functions from within this new script.The problem:1. Script A crashes when called from Script B exactly at the creation of a QPixmap object. Python console restarts.2. Script A runs fine when it is run on its own, but if I try to call it from Command Prompt python.exe crashes.I will explain and display the code in the next section as the best I have managed to do is identify the problems.What I have tried:The following is Script B. All it does is it imports Script A, creates an instance of a class defined in A, and uses that to call a method of that object.import Script A as eta = "sample"mdbPath = "C:\\Python27\\test.mdb"directory = "C:\\Python27"ui = et.HawkAutomationScript()ui.Main(mdbPath,directory,a)Script A contains the definition of a class called HawkAutomationScript. This object contains two functions to note: one Main function and one function to generate the report. These are the imported modules:from random import*import sysimport osimport subprocessimport timefrom datetime import datetimefrom PyQt4 import QtCore, QtGuifrom PyQt4.QtCore import QRectfrom PyQt4.QtGui import QTextDocument, QPrinter, QApplication, QPainterfrom string import Templateimport shutilimport timeimport pyodbcThis is the definition of the class and the Main function:doc = QTextDocument()myCursor = QtGui.QTextCursor(doc)class HawkAutomationScript(object): def Main(self,mdb,directory,name): Ui_FormObject = HawkAutomationScript()## Filling in the Report ## Ui_FormObject.fnGenerateReport(directory,name)def fnGenerateReport(self,directory,name): now = datetime.now() dateString = now.strftime("%d-%b-%Y") # For getting Current Date dirPath = "C:\\Users\\michael\\Downloads\\Ethernet\\scripts\\" ResultName = "SelfTest_Results" testName = name+".pdf" subDirPath = dirPath + name if(os.path.isdir(dirPath)): if(os.path.isdir(subDirPath)): subDirPath = subDirPath + testName else: os.mkdir(subDirPath) subDirPath = subDirPath + testName else: os.mkdir(dirPath) os.mkdir(subDirPath) subDirPath = subDirPath + testName pixmap = QtGui.QPixmap(doc.size().width(), doc.size().height()) ################ printer = QPrinter() printer.setOutputFileName(subDirPath) printer.setOutputFormat(QPrinter.PdfFormat) doc.print_(printer) # Create a QPainter to draw our content painter = QPainter(pixmap) painter.begin( printer ) doc.drawContents(painter) painter.end()This is all the relevant code.If I run script B, A crashes at the QPixmap line marked in the code. But if I copy the lines from B and run A on its own, the PDF report is generated. But if I run A on its own from Command Prompt python.exe crashes.Any help would be really appreciated. Thank you!! 解决方案 The following code looks wrong to me:dirPath = "C:\\Users\\michael\\Downloads\\Ethernet\\scripts\\"ResultName = "SelfTest_Results"testName = name+".pdf"subDirPath = dirPath # this is now the same as dirPathif(os.path.isdir(dirPath)): if(os.path.isdir(subDirPath)): # if both paths (actually only one) exists subDirPath = subDirPath + testName # append testName, but see below else: // this can never happen os.mkdir(subDirPath) subDirPath = subDirPath + testNameelse: os.mkdir(dirPath) # dirPath did not exist, so create it os.mkdir(subDirPath) # now create it again ??? subDirPath = subDirPath + testName # now append testName, but it's too late.[edit]What it should be:dirPath = "C:\\Users\\michael\\Downloads\\Ethernet\\scripts\\"ResultName = "SelfTest_Results"testName = name+".pdf"subDirPath = dirPath + name # append name, testName is the name of the PDF fileif(!os.path.isdir(dirPath)): os.mkdir(dirPath) # dirPath did not exist, so create itif(!os.path.isdir(subDirPath)): # subDirPath did not exist, so create it os.mkdir(subDirPath)...[/edit] 这篇关于从第二个脚本调用时,Python脚本崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-14 18:00