Created
February 25, 2016 11:30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
__author__ = 'SakuraCrowd' | |
""" | |
Drag & Drop した複数のファイルパスを処理するためのウィンドウです。 | |
ファイルパスはリストに表示され、コンテキストメニューから削除することができます。 | |
ファイルパスの処理は DragAndDropWindow.runHandler に追加してください。 | |
追加する前は、 Run を押すとファイルパスがログに表示される処理だけ実装してあります。 | |
ログに文字列を表示させる場合は DragAndDropWindow.addLog を | |
self.addLog('xxx') のように呼び出してください。 | |
ターミナルから次のコマンドで起動できます。 | |
python DragAndDropAndRunGUI.py | |
raspbian でファイルアイコンのダブルクリックで実行したい場合は、 | |
次のような sh を作り、その sh のファイルのプロパティのパーミッションの実行をなし以外にしてください。 | |
#!/bin/bash | |
python DragAndDropAndRunGUI.py | |
windows の bat の場合は次のように利用できます。 | |
START C:\~\Python34\pythonw.exe DragAndDropAndRunGUI.py | |
""" | |
import sys | |
import os | |
from PySide import QtGui | |
import subprocess | |
from subprocess import PIPE, CalledProcessError, check_call, Popen | |
import PySide | |
def bash_command(cmd): | |
""" | |
@brief sh コマンドを実行します。 | |
@param cmd コマンド文字列 | |
@return (成否(True=成功, False=失敗), メッセージ) | |
""" | |
try: | |
output = subprocess.check_output(cmd, shell=True, executable='/bin/sh', stderr=subprocess.STDOUT) | |
except CalledProcessError as e: | |
return (False, e.output) | |
return (True, output) | |
class DragAndDropWindow(QtGui.QWidget): | |
def __init__(self): | |
QtGui.QWidget.__init__(self) | |
layout1 = QtGui.QVBoxLayout() | |
self.setLayout(layout1) | |
# ファイル一覧 | |
self.listUrl = QtGui.QListWidget(self) | |
self.listUrl.setContextMenuPolicy(PySide.QtCore.Qt.CustomContextMenu) | |
self.listUrl.customContextMenuRequested.connect(self.showMenuListUrl) | |
layout1.addWidget(self.listUrl) | |
layout1_1 = QtGui.QHBoxLayout() | |
layout1.addLayout(layout1_1) | |
# ユニーク URL チェックボックス | |
self.checkboxUniqueUrl = QtGui.QCheckBox('Unique URL', self) | |
self.checkboxUniqueUrl.setChecked(True) | |
layout1_1.addWidget(self.checkboxUniqueUrl) | |
# フォルダ禁止のチェックボックス | |
self.checkboxFileOnly = QtGui.QCheckBox('File Only', self) | |
layout1_1.addWidget(self.checkboxFileOnly) | |
# 実行ボタン | |
self.buttonRun = QtGui.QPushButton('Run', self) | |
self.buttonRun.clicked.connect(self.runHandler) | |
layout1_1.addWidget(self.buttonRun) | |
# ログ | |
self.textLog = QtGui.QTextEdit(self) | |
self.textLog.setReadOnly(True) | |
self.textLog.setContextMenuPolicy(PySide.QtCore.Qt.CustomContextMenu) | |
self.textLog.customContextMenuRequested.connect(self.showMenuTextLog) | |
layout1.addWidget(self.textLog) | |
# ドロップ許可 | |
self.setAcceptDrops(True) | |
return | |
def showMenuListUrl(self, event): | |
menu = QtGui.QMenu() | |
menu.addAction("Remove", self.removeUrl) | |
menu.exec_(QtGui.QCursor.pos()) | |
return | |
def showMenuTextLog(self, event): | |
menu = self.textLog.createStandardContextMenu() | |
action = menu.addAction("Clear all log", self.clearAllLog) | |
menu.exec_(QtGui.QCursor.pos()) | |
return | |
def removeUrl(self): | |
for item in self.listUrl.selectedItems(): | |
self.listUrl.takeItem(self.listUrl.row(item)) | |
return | |
def clearAllLog(self): | |
self.textLog.clear() | |
return | |
def addLog(self, strLog): | |
self.textLog.moveCursor(QtGui.QTextCursor.End) | |
self.textLog.insertPlainText(strLog) | |
self.textLog.moveCursor(QtGui.QTextCursor.End) | |
return | |
def dropEvent(self, event): | |
mimedata = event.mimeData() | |
urllist = mimedata.urls() | |
self.addLog("add URLs" + u'\u2029') | |
for i in urllist: | |
flgAdd = True | |
strPreMsg = ' OK \t>' | |
if self.checkboxUniqueUrl.isChecked() == True: | |
# すでにリストに存在するパスは追加しない。 | |
for row in range(0, self.listUrl.count()): | |
if i.path() == self.listUrl.item(row).text(): | |
flgAdd = False | |
strPreMsg = ' NG exist\t>' | |
if self.checkboxFileOnly.isChecked() == True: | |
if os.path.isdir(i.path()) == True: | |
flgAdd = False | |
strPreMsg = ' NG folder\t>' | |
self.addLog(strPreMsg + i.path() + u'\u2029') | |
if flgAdd == True: | |
self.listUrl.addItem(i.path()) | |
return | |
def dragEnterEvent(self, event): | |
mime = event.mimeData() | |
if mime.hasUrls() == True: | |
event.accept() | |
else: | |
event.ignore() | |
return | |
def closeEvent(self, event): | |
""" | |
window 終了の直前のイベント | |
""" | |
return | |
def runHandler(self): | |
""" | |
ファイルリストから任意の処理を実行する | |
""" | |
for row in range(0, self.listUrl.count()): | |
targetUrl = self.listUrl.item(row).text() | |
self.addLog('process > ' + targetUrl + u'\u2029') | |
return | |
app = QtGui.QApplication(sys.argv) | |
argc = len(sys.argv) | |
win = DragAndDropWindow() | |
win.resize(640, 320) | |
win.setWindowTitle("DragAndDropAndRun Window") | |
win.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://sakuracrowd.blogspot.jp/2016/02/raspberrypi2pythonpyside.html