- QFileDialog
- QMessageBox
- QInputDialog
- QColorDialog
- QFontDialog
-
grp = QtWidgets.QGroupBox("QMessageBox")
image.png
from PySide2 import QtCore, QtWidgets
from shiboken2 import wrapInstance
import maya.OpenMayaUI as omui
import maya.cmds as cmds
import pymel.core as pm
def get_maya_main_window():
'''
Maya main window widget as a pyside2 python object
:return:
'''
maya_main_wnd = omui.MQtUtil.mainWindow()
return wrapInstance(long(maya_main_wnd), QtWidgets.QWidget)
class ExampleDialog(QtWidgets.QDialog):
FILE_FILTERS = 'Maya(*.ma *.mb);;Maya ASCII(*.ma);;Maya Binary(*.mb);;All Files(*.*)'
selected_filter = 'All Files(*.*)'
def __init__(self, parent=get_maya_main_window()):
super(ExampleDialog, self).__init__(parent)
self.setWindowTitle('Standard Qt Dialog UI')
# self.setMinimumWidth(200)
self.setWindowFlags(self.windowFlags())
self.prefs_directory = cmds.internalVar(userPrefDir = True)
self.create_widgets()
self.create_layouts()
self.create_connections()
# QtCore.QMetaObject.connectSlotsByName(self)
def create_widgets(self):
# QFile_btn
self.get_existing_dir_btn = QtWidgets.QPushButton('getExistingDirectory')
self.get_open_file_name_btn = QtWidgets.QPushButton('getOpenFileName')
self.get_open_file_names_btn = QtWidgets.QPushButton('getOpenFileNames')
self.get_save_file_name_btn = QtWidgets.QPushButton('getSaveFileName')
# QMessageBox_btn
self.critical_btn = QtWidgets.QPushButton('critical')
self.warning_btn = QtWidgets.QPushButton('waring')
self.information_btn = QtWidgets.QPushButton('information')
self.question_btn = QtWidgets.QPushButton('question')
# QInput_btn
self.get_double_btn = QtWidgets.QPushButton("getDouble")
self.get_int_btn = QtWidgets.QPushButton("getInt")
self.get_text_btn = QtWidgets.QPushButton("getText")
self.get_multi_line_text_btn = QtWidgets.QPushButton("getMultiLineText")
# QColorDialog_btn
self.get_color_btn = QtWidgets.QPushButton("getColor")
# QFontDialog_btn
self.get_font_btn = QtWidgets.QPushButton("getFont")
def create_layouts(self):
main_layout = QtWidgets.QVBoxLayout(self)
# QFileDialog_layout
grp_layout = QtWidgets.QHBoxLayout()
grp_layout.addWidget(self.get_existing_dir_btn)
grp_layout.addWidget(self.get_open_file_name_btn)
grp_layout.addWidget(self.get_open_file_names_btn)
grp_layout.addWidget(self.get_save_file_name_btn)
grp_layout.addStretch()
grp = QtWidgets.QGroupBox("QFileDialog")
grp.setLayout(grp_layout)
main_layout.addWidget(grp)
# QMessageBox_layout
grp_layout = QtWidgets.QHBoxLayout()
grp_layout.addWidget(self.critical_btn)
grp_layout.addWidget(self.warning_btn)
grp_layout.addWidget(self.information_btn)
grp_layout.addWidget(self.question_btn)
grp_layout.addStretch()
grp = QtWidgets.QGroupBox("QMessageBox")
grp.setLayout(grp_layout)
main_layout.addWidget(grp)
# QInput_layout
grp_layout = QtWidgets.QHBoxLayout()
grp_layout.addWidget(self.get_double_btn)
grp_layout.addWidget(self.get_int_btn)
grp_layout.addWidget(self.get_text_btn)
grp_layout.addWidget(self.get_multi_line_text_btn)
grp_layout.addStretch()
grp = QtWidgets.QGroupBox("QInputDialog")
grp.setLayout(grp_layout)
main_layout.addWidget(grp)
# QColorDialog_layout
grp_layout = QtWidgets.QHBoxLayout()
grp_layout.addWidget(self.get_color_btn)
grp_layout.addStretch()
grp = QtWidgets.QGroupBox("QColorDialog")
grp.setLayout(grp_layout)
main_layout.addWidget(grp)
# QFontDialog_layout
grp_layout = QtWidgets.QHBoxLayout()
grp_layout.addWidget(self.get_font_btn)
grp_layout.addStretch()
grp = QtWidgets.QGroupBox("QFontDialog")
grp.setLayout(grp_layout)
main_layout.addWidget(grp)
main_layout.addStretch()
def create_connections(self):
self.get_existing_dir_btn.clicked.connect(self.get_existing_directory)
self.get_open_file_name_btn.clicked.connect(self.get_openFile_name)
self.get_open_file_names_btn.clicked.connect(self.get_openFile_names)
self.get_save_file_name_btn.clicked.connect(self.get_saveFile_name)
self.critical_btn.clicked.connect(self.critical)
self.warning_btn.clicked.connect(self.warning_)
self.information_btn.clicked.connect(self.information)
self.question_btn.clicked.connect(self.question)
self.get_double_btn.clicked.connect(self.get_float)
self.get_int_btn.clicked.connect(self.get_int)
self.get_text_btn.clicked.connect(self.get_text)
self.get_multi_line_text_btn.clicked.connect(self.get_multi_line_text)
self.get_color_btn.clicked.connect(self.get_color)
self.get_font_btn.clicked.connect(self.get_font)
# ___Slots___
# QFile
def get_existing_directory(self):
directory = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select Directory',
self.prefs_directory)
if directory:
print 'File path: {}'.format(directory)
def get_openFile_name(self):
file_path, self.selected_filter = QtWidgets.QFileDialog.getOpenFileName(self, "Select File", "",
self.FILE_FILTERS,
self.selected_filter)
if file_path:
print 'File path: {}'.format(file_path)
def get_openFile_names(self):
file_paths, self.selected_filter = QtWidgets.QFileDialog.getOpenFileNames(self, "Select File", "",
self.FILE_FILTERS,
self.selected_filter)
if file_paths:
print 'File path: {}'.format(file_paths)
def get_saveFile_name(self):
file_path, self.selected_filter = QtWidgets.QFileDialog.getSaveFileName(self, "Save File", "",
self.FILE_FILTERS,
self.selected_filter)
if file_path:
print 'File path: {}'.format(file_path)
#QMessageBox
def critical(self):
QtWidgets.QMessageBox.critical(self, "Error", "There was an Error somewhere")
def warning_(self):
QtWidgets.QMessageBox.warning(self, "Waring", 'Something odd just happened')
def information(self):
QtWidgets.QMessageBox.information(self, "Info", "Task completed successfully")
def question(self):
QtWidgets.QMessageBox.question(self, 'Question', 'Would you like to continue?')
# QInput
def get_float(self):
value, ok = QtWidgets.QInputDialog.getDouble(self, 'Enter a Value', 'Value:')
if ok:
print value
def get_int(self):
value, ok = QtWidgets.QInputDialog.getInt(self, 'Enter a Value', 'Value:')
if ok:
print value
def get_text(self):
text, ok = QtWidgets.QInputDialog.getText(self, 'Enter Text', 'Text:')
if ok:
print text
def get_multi_line_text(self):
text, ok = QtWidgets.QInputDialog.getMultiLineText(self, 'Enter Text', 'Text:')
if ok:
print text
# QColor
def get_color(self):
color = QtWidgets.QColorDialog.getColor(parent=self)
if color:
print("Red:{0} Green:{1} Blue:{2}".format(color.red(), color.green(), color.blue()))
def get_font(self):
font, ok = QtWidgets.QFontDialog.getFont(parent=self)
if ok:
print("Family: {0} Point Size: {1}".format(font.family(), font.pointSize()))
if __name__ == '__main__':
try:
myTest_ui.close() # pylint:disable = E0601
myTest_ui.deleteLater() # pylint:disable = E0601
except:
pass
myTest_ui = ExampleDialog()
myTest_ui.show()