Skip to content

Instantly share code, notes, and snippets.

@csaez
Last active August 2, 2019 13:37
Show Gist options
  • Save csaez/599c321d7b2cc4a3ff40 to your computer and use it in GitHub Desktop.
Save csaez/599c321d7b2cc4a3ff40 to your computer and use it in GitHub Desktop.
Maya: A simple qt gui that can be ran within Maya or as standalone (notice the shebang)
#!/usr/autodesk/maya/bin/mayapy
import sys
for each in ("PySide", "PySide2"):
try:
if each == "PySide2":
_temp = __import__(each, globals(), locals(), ['QtWidgets'], -1)
QtWidgets = _temp.QtWidgets
else:
_temp = __import__(each, globals(), locals(), ('QtGui'), -1)
QtWidgets = _temp.QtGui
_temp = __import__(each, globals(), locals(), ('QtCore'), -1)
QtCore = _temp.QtCore
QtGui = QtWidgets
except ImportError:
pass
else:
break
def mainWindow(widget=None):
widget = widget or QtGui.QApplication.activeWindow()
if widget is None:
return
parent = widget.parent()
if parent is None:
return widget
return mainWindow(parent)
def isStandAlone():
return mainWindow() is None
def onClicked(button, label):
label.setText("Good job!")
button.setText("Try again ;)")
button.clicked.connect(button.parent().parent().close)
def main(parent=None):
win = QtGui.QMainWindow(parent)
# setup ui
label = QtGui.QLabel("Hi there, what's up?")
f = label.font()
f.setPointSize(16)
label.setFont(f)
button = QtGui.QPushButton("Click me!")
splitter = QtGui.QSplitter(QtCore.Qt.Orientation.Vertical)
layout = QtGui.QVBoxLayout()
layout.addWidget(label)
layout.addWidget(button)
layout.addWidget(splitter)
centralWidget = QtGui.QWidget()
centralWidget.setLayout(layout)
win.setCentralWidget(centralWidget)
# manage signal/slots
button.clicked.connect(lambda: onClicked(button, label))
return win
if __name__ == '__main__':
standalone = isStandAlone()
if standalone:
app = QtGui.QApplication(sys.argv)
win = main(mainWindow())
win.show()
if standalone:
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment