I've been coding a OCR book scanning thing (it renames pages by reading the page number), and have switched to a GUI from my basic CLI Python script.
I'm using PyQT4 and looked at a ton of documents on drag and drop, but no luck. It just refuses to take those files! I was using these to articles for my UI design:
http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/
http://zetcode.com/tutorials/pyqt4/dragdrop/
I noticed that there are a TON of ways to setup a PyQT4 GUI. Which one works the best?
Oops, here's the source code for the project.
The main script:
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtGui import QListWidget
from layout import Ui_window
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_window()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.listWidget, QtCore.SIGNAL("dropped"), self.picture_dropped)
def picture_dropped(self, l):
for url in l:
if os.path.exists(url):
picture = Image.open(url)
picture.thumbnail((72, 72), Image.ANTIALIAS)
icon = QIcon(QPixmap.fromImage(ImageQt.ImageQt(picture)))
item = QListWidgetItem(os.path.basename(url)[:20] + "...", self.pictureListWidget)
item.setStatusTip(url)
item.setIcon(icon)
class DragDropListWidget(QListWidget):
def __init__(self, type, parent = None):
super(DragDropListWidget, self).__init__(parent)
self.setAcceptDrops(True)
self.setIconSize(QSize(72, 72))
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
l = []
for url in event.mimeData().urls():
l.append(str(url.toLocalFile()))
self.emit(SIGNAL("dropped"), l)
else:
event.ignore()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
And the UI file...
# Form implementation generated from reading ui file 'layout.ui'
#
# Created: Thu Nov 11 00:22:52 2010
# by: PyQt4 UI code generator 4.8.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_window(object):
def setupUi(self, window):
window.setObjectName(_fromUtf8("window"))
window.resize(543, 402)
window.setAcceptDrops(True)
self.centralwidget = QtGui.QWidget(window)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.listWidget = QtGui.QListWidget(self.centralwidget)
self.listWidget.setProperty(_fromUtf8("cursor"), QtCore.Qt.SizeHorCursor)
self.listWidget.setAcceptDrops(True)
self.listWidget.setObjectName(_fromUtf8("listWidget"))
self.verticalLayout.addWidget(self.listWidget)
window.setCentralWidget(self.centralwidget)
self.retranslateUi(window)
QtCore.QMetaObject.connectSlotsByName(window)
def retranslateUi(self, window):
window.setWindowTitle(QtGui.QApplication.translate("window", "PyNamer OCR", None, QtGui.QApplication.UnicodeUTF8))
Thanks to anybody who can help!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…