0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PySideでUI作るときのテンプレート

Posted at

プロンプトの勉強になった

# -*- coding: utf-8 -*-
from maya import OpenMayaUI
from PySide2 import QtWidgets, QtCore
import shiboken2 as shiboken
import maya.cmds as cmds

class MyToolUI(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(MyToolUI, self).__init__(parent)
        self.setWindowTitle("My Tool")
        self.setMinimumSize(400, 700)
        self.create_ui()
        self.create_connections()
        self.set_styles()

    def create_ui(self):
        main_layout = QtWidgets.QVBoxLayout(self)
        
        # メイングループボックス
        self.main_groupbox = self.create_main_groupbox()
        main_layout.addWidget(self.main_groupbox)

        # その他のウィジェット
        self.textedit = QtWidgets.QTextEdit()
        main_layout.addWidget(QtWidgets.QLabel("TextEdit:"))
        main_layout.addWidget(self.textedit)

        # タブウィジェット
        self.tabwidget = self.create_tabwidget()
        main_layout.addWidget(QtWidgets.QLabel("TabWidget:"))
        main_layout.addWidget(self.tabwidget)

        # 追加のグループボックス
        self.groupbox1 = self.create_groupbox("Group Box 1", "Group 1 Button")
        self.groupbox2 = self.create_groupbox("Group Box 2", "Group 2 Button")
        main_layout.addWidget(self.groupbox1)
        main_layout.addWidget(self.groupbox2)

        # Unused progressbar (kept for future reference)
        # self.progressbar = QtWidgets.QProgressBar()
        # self.progressbar.setValue(50)
        # main_layout.addWidget(QtWidgets.QLabel("ProgressBar:"))
        # main_layout.addWidget(self.progressbar)

    def create_main_groupbox(self):
        groupbox = QtWidgets.QGroupBox("Main Controls")
        layout = QtWidgets.QVBoxLayout()

        self.checkbox = QtWidgets.QCheckBox("Enable Option")
        self.radio1 = QtWidgets.QRadioButton("Option 1")
        self.radio2 = QtWidgets.QRadioButton("Option 2")
        self.combobox = QtWidgets.QComboBox()
        self.combobox.addItems(["Item 1", "Item 2", "Item 3"])
        self.lineedit = QtWidgets.QLineEdit()
        self.spinbox = QtWidgets.QSpinBox()
        self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)

        layout.addWidget(QtWidgets.QLabel("Checkbox:"))
        layout.addWidget(self.checkbox)
        layout.addWidget(QtWidgets.QLabel("Radio Buttons:"))
        radio_layout = QtWidgets.QHBoxLayout()
        radio_layout.addWidget(self.radio1)
        radio_layout.addWidget(self.radio2)
        layout.addLayout(radio_layout)
        layout.addWidget(QtWidgets.QLabel("Combobox:"))
        layout.addWidget(self.combobox)
        layout.addWidget(QtWidgets.QLabel("LineEdit:"))
        layout.addWidget(self.lineedit)
        layout.addWidget(QtWidgets.QLabel("SpinBox:"))
        layout.addWidget(self.spinbox)
        layout.addWidget(QtWidgets.QLabel("Slider:"))
        layout.addWidget(self.slider)

        groupbox.setLayout(layout)
        return groupbox

    def create_tabwidget(self):
        tabwidget = QtWidgets.QTabWidget()
        
        # Tab 1
        tab1 = QtWidgets.QWidget()
        tab1_layout = QtWidgets.QVBoxLayout(tab1)
        self.tab1_button = QtWidgets.QPushButton("Click Me")
        tab1_layout.addWidget(self.tab1_button)
        tabwidget.addTab(tab1, "Tab 1")

        # Tab 2
        tab2 = QtWidgets.QWidget()
        tab2_layout = QtWidgets.QVBoxLayout(tab2)
        self.tab2_checkbox = QtWidgets.QCheckBox("Tab 2 Option")
        tab2_layout.addWidget(self.tab2_checkbox)
        tabwidget.addTab(tab2, "Tab 2")

        return tabwidget

    def create_groupbox(self, title, button_text):
        groupbox = QtWidgets.QGroupBox(title)
        layout = QtWidgets.QVBoxLayout()
        button = QtWidgets.QPushButton(button_text)
        layout.addWidget(button)
        groupbox.setLayout(layout)
        return groupbox

    def create_connections(self):
        self.tab1_button.clicked.connect(self.on_tab1_button_click)
        self.groupbox1.findChild(QtWidgets.QPushButton).clicked.connect(self.on_groupbox1_button_click)
        self.groupbox2.findChild(QtWidgets.QPushButton).clicked.connect(self.on_groupbox2_button_click)

    def set_styles(self):
        self.setStyleSheet(
            '''
            QGroupBox {
                border: 2px solid #256d7b;
                border-radius: 6px;
                margin-top: 2em;
                font-size: 12px;
            } 

            QGroupBox::title {
                color: #fff;
                background-color: #256d7b;
                subcontrol-origin: margin;
                padding: 0.5em 0.7em;
                border-top-left-radius: 6px;
                border-top-right-radius: 6px;
            }
            '''
        )

    def on_tab1_button_click(self):
        cmds.warning("Tab 1 Button clicked!")

    def on_groupbox1_button_click(self):
        cmds.warning("Group Box 1 Button clicked!")

    def on_groupbox2_button_click(self):
        cmds.warning("Group Box 2 Button clicked!")

def maya_main_window():
    main_window_ptr = OpenMayaUI.MQtUtil.mainWindow()
    return shiboken.wrapInstance(int(main_window_ptr), QtWidgets.QWidget)

def show_ui():
    ui = MyToolUI(parent=maya_main_window())
    ui.show()

# UIを表示
if __name__ == "__main__":
    show_ui()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?