1. #!/usr/autodesk/maya/bin/mayapy
  2. import sys
  3. for each in ("PySide", "PySide2"):
  4. try:
  5. if each == "PySide2":
  6. _temp = __import__(each, globals(), locals(), ['QtWidgets'], -1)
  7. QtWidgets = _temp.QtWidgets
  8. else:
  9. _temp = __import__(each, globals(), locals(), ('QtGui'), -1)
  10. QtWidgets = _temp.QtGui
  11. _temp = __import__(each, globals(), locals(), ('QtCore'), -1)
  12. QtCore = _temp.QtCore
  13. QtGui = QtWidgets
  14. except ImportError:
  15. pass
  16. else:
  17. break
  18. def mainWindow(widget=None):
  19. widget = widget or QtGui.QApplication.activeWindow()
  20. if widget is None:
  21. return
  22. parent = widget.parent()
  23. if parent is None:
  24. return widget
  25. return mainWindow(parent)
  26. def isStandAlone():
  27. return mainWindow() is None
  28. def onClicked(button, label):
  29. label.setText("Good job!")
  30. button.setText("Try again ;)")
  31. button.clicked.connect(button.parent().parent().close)
  32. def main(parent=None):
  33. win = QtGui.QMainWindow(parent)
  34. # setup ui
  35. label = QtGui.QLabel("Hi there, what's up?")
  36. f = label.font()
  37. f.setPointSize(16)
  38. label.setFont(f)
  39. button = QtGui.QPushButton("Click me!")
  40. splitter = QtGui.QSplitter(QtCore.Qt.Orientation.Vertical)
  41. layout = QtGui.QVBoxLayout()
  42. layout.addWidget(label)
  43. layout.addWidget(button)
  44. layout.addWidget(splitter)
  45. centralWidget = QtGui.QWidget()
  46. centralWidget.setLayout(layout)
  47. win.setCentralWidget(centralWidget)
  48. # manage signal/slots
  49. button.clicked.connect(lambda: onClicked(button, label))
  50. return win
  51. if __name__ == '__main__':
  52. standalone = isStandAlone()
  53. if standalone:
  54. app = QtGui.QApplication(sys.argv)
  55. win = main(mainWindow())
  56. win.show()
  57. if standalone:
  58. sys.exit(app.exec_())
文档更新时间: 2018-06-18 06:29   作者:刘电