# -*- coding: utf-8 -*-
"""
Author: Administrator
Date: 2020/5/14 22:41
"""
from PySide2 import QtCore
from PySide2 import QtWidgets
from shiboken2 import wrapInstance
import maya.OpenMayaUI as omui
import maya.cmds as cmds
import maya.mel as mel
import time
def maya_main_window():
"""
Return the Maya main window widget as a Python object
"""
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(long(main_window_ptr), QtWidgets.QWidget)
class CustomDialog(QtWidgets.QDialog):
WINDOW_TITLE = "Progress Bar"
dlg_instance = None
def __init__(self, parent=maya_main_window()):
super(CustomDialog, self).__init__(parent)
self.setWindowTitle(self.WINDOW_TITLE)
self.setMinimumSize(300, 100)
self.setWindowFlags(self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint)
self.cancel_progress = False
self.create_widgets()
self.create_layout()
self.create_connections()
def create_widgets(self):
self.progress_bar_label = QtWidgets.QLabel("Progress waiting...")
self.progress_bar = QtWidgets.QProgressBar()
self.progress_bar.setValue(0)
self.progress_bar.setRange(0, 10)
self.run_progress_bttn = QtWidgets.QPushButton("Do It")
self.cancel_bttn = QtWidgets.QPushButton("Cancel")
self.update_visibility()
def create_layout(self):
progress_layout = QtWidgets.QVBoxLayout()
progress_layout.addWidget(self.progress_bar_label)
progress_layout.addWidget(self.progress_bar)
bttn_layout = QtWidgets.QHBoxLayout()
bttn_layout.addStretch()
bttn_layout.addWidget(self.run_progress_bttn)
bttn_layout.addWidget(self.cancel_bttn)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.setContentsMargins(5, 5, 5, 5)
main_layout.addLayout(progress_layout)
main_layout.addLayout(bttn_layout)
def create_connections(self):
self.run_progress_bttn.clicked.connect(self.run_progress_bar)
self.cancel_bttn.clicked.connect(self.canel)
def run_progress_bar(self):
self.cancel_progress = True
self.update_visibility()
num = 10
for i in range(1, num + 1):
if not self.cancel_progress:
break
self.progress_bar_label.setText("Progress {0}/{1}".format(i, num))
self.progress_bar.setValue(i)
time.sleep(0.5)
QtCore.QCoreApplication.processEvents()
self.cancel_progress = False
self.update_visibility()
def canel(self):
self.cancel_progress = False
def update_visibility(self):
self.run_progress_bttn.setVisible(not self.cancel_progress)
self.progress_bar.setVisible(self.cancel_progress)
self.progress_bar_label.setVisible(self.cancel_progress)
self.cancel_bttn.setVisible(self.cancel_progress)
def showWindow():
global dialog
try:
dialog.close()
dialog.deleteLater()
except:
pass
dialog = CustomDialog()
dialog.show()
if __name__ == "__main__":
try:
dialog.close()
dialog.deleteLater()
except:
pass
dialog = CustomDialog()
dialog.show()
QProgressBar
最新推荐文章于 2023-11-05 16:14:54 发布