QApplicationをコードで自動で閉じようとするときに出たエラーの解決方法です。
PySide2.QtWidgets.QApplication’ object has no attribute ‘close’
Question : QMainWindow
class DF_TalkUI(QMainWindow):
def __init__(self, parent = None):
Answer : QWidget
class DF_TalkUI(QWidget):
def __init__(self, parent = None):
Fix Code
#------------------------------------------------------------------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
import os
from functools import partial
import time
import imp
"""
PySide2モジュールを探し、ある場合はそちらをインポートします。
"""
try:
imp.find_module('PySide2')
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
except ImportError:
from PySide.QtGui import *
from PySide.QtCore import *
LOGO_IMAGE = r"画像のパスをここに入れてください。"
def get_maya_pointer():
"""
Mayaのメインウィンドウを取得する関数
"""
try:
import maya.cmds as cmds
from maya import OpenMayaUI
except ImportError:
return None
"""
実は2017ではshibokenも2になっているので、あればshiboken2をインポートします。
"""
try:
imp.find_module("shiboken2")
import shiboken2
return shiboken2.wrapInstance(int(OpenMayaUI.MQtUtil.mainWindow()), QWidget)
except ImportError:
import shiboken
return shiboken.wrapInstance(int(OpenMayaUI.MQtUtil.mainWindow()), QWidget)
class DF_TalkUI(QWidget):
def __init__(self, parent = None):
super(DF_TalkUI, self).__init__(parent)
self.setObjectName("DF_Talk_Window")
self.setWindowTitle("DF Talk Window")
#self._initUI()
self.errorDialog = QErrorMessage(self) # QErrorMessageインスタンスの保持
self.Window = self
def myInit(self,qApp,refPath):
self.qApp=qApp
self.wrapper=wrapper = QWidget()
#self.setCentralWidget(wrapper)
mainLayout = QVBoxLayout()
self.mainLayout= mainLayout
wrapper.setLayout(mainLayout)
#-----------------------------------------------------------------------
# sixth row
#-----------------------------------------------------------------------
sixthHorizontalArea = QHBoxLayout()
sixthHorizontalArea.setSpacing(20)
mainLayout.addLayout(sixthHorizontalArea)
"""
progressDialogBtn = QPushButton("Progress Dialog")
sixthHorizontalArea.addWidget(progressDialogBtn)
progressDialogBtn.clicked.connect(self.havyfunc)
"""
#---------------------------
self.havyfunc(refPath)
#-----------------------------
#-----------------------------------------------------------------------
# seventh row
#-----------------------------------------------------------------------
#self.havyfunc()
def _showProgressDialog(self):
print("---------------------ProgressBar Starting--------------------------")
self.max = 100
self.progressDialog = QProgressDialog("Progress...", "Cancel", 0, self.max, self)
self.progressDialog.setWindowTitle("Progress Dialog")
self.progressDialog.show()
def setProgress(self,count):
print("---------------------setProgress ( "+str(count)+"/100 )--------------------------")
self.qApp.processEvents()
if self.progressDialog.wasCanceled():
self.progressDialog.setValue(100)
self.progressDialog.setValue(count)
self.progressDialog.setLabelText("Progress... %d %%" % count)
def havyfunc(self,refPath):
#--------------------------------------------------
self._showProgressDialog()
self.setProgress(1)
#--------------------------------------------------
duplicatedMesh_vtx_arr=[1,2,3,4,5]
duplicatedMesh_vtx_arr_len=len(duplicatedMesh_vtx_arr)
cmds.file(refPath, ignoreVersion=1, type="mayaAscii", namespace=":", r=1, gl=1, mergeNamespacesOnClash=False, options="v=0;")
"""
for i in range(0,duplicatedMesh_vtx_arr_len):
#---------------------------------------------------
parcent=(100*i/duplicatedMesh_vtx_arr_len) /2
self.setProgress(parcent)
print("setProgress("+str(parcent)+")")
time.sleep(0.1)
#---------------------------------------------------
#重たい処理1
for i in range(0,duplicatedMesh_vtx_arr_len):
#---------------------------------------------------
parcent=(100*i/duplicatedMesh_vtx_arr_len) /2 +50
self.setProgress(parcent)
print("setProgress("+str(parcent)+")")
time.sleep(0.1)
#---------------------------------------------------
#重たい処理2
"""
#--------------------------------------
self.setProgress(100)
print("setProgress("+str(100)+")")
self.MyHide()
#self.closeEvent(QCloseEvent)
#--------------------------------------
def MyHide(self):
self.resize(2, 1) # 幅800、高さ600
self.Window.move(10000, 10000) #X 10000 Y 10000
"""
#self.mainLayout.close()
self.wrapper.hide()
self.wrapper.close()
self.Window.hide()
self.Window.close()
self.close()
"""
def _makeHorizontalLine(self):
hline = QFrame()
hline.setFrameShape(QFrame.HLine)
hline.setFrameShadow(QFrame.Sunken)
return hline
def start(app,refPath):
maya_win = get_maya_pointer()
ui = DF_TalkUI(parent = maya_win)
ui.myInit(app,refPath)
ui.show()
return ui
def starter(refPath):
app = QApplication.instance()
if app is None:
app = QApplication([])
ui = start(app,refPath)
app.exec_()
#=----------------------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
from maya import cmds
import pymel.core as pm
def mosa_anim_func10_2to10_3_reference(RigIDNum):
reference_path="D:/MOSADesign/WorkData/Avatar/" + RigIDNum + "/Motion/Rig/PL_" + RigIDNum + "_rig.ma"
#cmds.file(reference_path, ignoreVersion=1, type="mayaAscii", namespace=":", r=1, gl=1, mergeNamespacesOnClash=False, options="v=0;")
starter(reference_path)
print("__name__ = "+__name__ )
if __name__ == '__main__':
mosa_anim_func10_2to10_3_reference("005")
参考