LoginSignup
1
0

More than 1 year has passed since last update.

PyQt5のチュートリアルを動かす ➈ Multiple Document Interface

Posted at

はじめに

今回はウィンドウ内コンテナに複数のウィジェットを持つMDIのデモを動作させました。リソースを少なくなるように内部でなっているそうですが,概念が難しく詳細は参考サイトをご覧ください。

デモ

multi_document.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class MainWindow(QMainWindow):
   count = 0

   def __init__(self, parent = None):
      super(MainWindow, self).__init__(parent)
      self.mdi = QMdiArea()
      self.setCentralWidget(self.mdi)
      bar = self.menuBar()

      file = bar.addMenu("File")
      file.addAction("New")
      file.addAction("cascade")
      file.addAction("Tiled")
      file.triggered[QAction].connect(self.windowaction)
      self.setWindowTitle("MDI demo")

   def windowaction(self, q):
      print ("triggered")

      if q.text() == "New":
         MainWindow.count = MainWindow.count+1
         sub = QMdiSubWindow()
         sub.setWidget(QTextEdit())
         sub.setWindowTitle("subwindow"+str(MainWindow.count))
         self.mdi.addSubWindow(sub)
         sub.show()

      if q.text() == "cascade":
         self.mdi.cascadeSubWindows()

      if q.text() == "Tiled":
         self.mdi.tileSubWindows()

def main():
   app = QApplication(sys.argv)
   ex = MainWindow()
   ex.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   main()

実行してウィンドウ追加をしていったり整列などのボタンを押したりすることで,window内のドキュメントウィンドウを増やしたり、整列させることができました。(若干動作がもっさりしている感じがしました...)

Screenshot from 2021-12-02 12-53-03.png

Screenshot from 2021-12-02 12-52-49.png

参考

1
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
1
0