2

フォルダーがあり、そのフォルダーにはファイルと他のフォルダーがあり、ファイルとフォルダーが含まれています。今、私がやろうとしているのは、ドロップダウンメニューを作成し、各ファイル名をメニューに追加することです。サブメニューを作成し、そのフォルダー内のファイル名をそのメニューに追加するなどです。いくつかの(不完全な)コードがあります:

def TemplatesSetup(self):

  # add templates menu
  template_menu = self.menubar.addMenu('&Templates')

  #temp = template_menu.addMenu()

  # check if templates folder exists
  if os.path.exists('templates/') is False:
    temp = QAction('Can not find templates folder...', self)
    temp.setDisabled (1)
    template_menu.addAction(temp)
    return

  for fulldir, folder, filename in os.walk('templates'):

    for f in filename:
      template_menu.addAction(QAction(f, self))

しかし、これを行う最善の方法がどのようになるかはまだわかりません。何か案は?

4

1 に答える 1

1

私はあなたのために完全な例を作りました。

import sys
import os
import os.path

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        template_menu = self.menuBar().addMenu('&Templates')
        menus = {'templates': template_menu}

        for dirpath, dirnames, filenames in os.walk('templates'):
            current = menus[dirpath]
            for dn in dirnames:
                menus[os.path.join(dirpath, dn)] = current.addMenu(dn)
            for fn in filenames:
                current.addAction(fn)

if __name__=='__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
于 2013-08-12T23:20:12.917 に答える