从第二个脚本

时间:2017-10-31 13:12:23

标签: python python-2.7 pyqt4 qpixmap

我有一个生成PDF报告的报告生成脚本(让我们称之为脚本A)。我想在一个新脚本(脚本B)中导入它,并从这个新脚本中调用这些函数。

问题:

  
      
  1. 脚本A在创建QPixmap对象时完全从脚本B调用时崩溃。 Python控制台重新启动。

  2.   
  3. 脚本A在单独运行时运行正常,但如果我尝试从命令提示符调用它,python.exe崩溃。

  4.   

以下是脚本B.它只是导入脚本A,创建A中定义的类的实例,并使用它来调用该对象的方法。

import Script A as et

a = "sample"
mdbPath = "C:\\Python27\\test.mdb"
directory = "C:\\Python27"

ui = et.HawkAutomationScript()
ui.Main(mdbPath,directory,a)

脚本A包含名为HawkAutomationScript的类的定义。该对象包含两个要注意的函数:一个Main函数和一个生成报告的函数。这些是导入的模块:

from random import*
import sys
import os
import subprocess
import time
from datetime import datetime
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QRect
from PyQt4.QtGui import QTextDocument, QPrinter, QApplication, QPainter
from string import Template
import shutil
import time
import pyodbc

这是类和Main函数的定义:

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:\\"

        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()

这是所有相关的代码,它是可以运行的。

如果我运行脚本B,A会在代码中标记的QPixmap行崩溃。但是,如果我从B复制行并自行运行A,则会生成PDF报告。但是如果我自己从命令提示符运行A,python.exe崩溃。

任何帮助都会非常感激。谢谢!!

0 个答案:

没有答案