简体   繁体   中英

How to display an image and prompt for a short string in Python from command line

(I edited the whole question to be more clear)


Hello,

I have never had any affairs with Python GUI libraries. I know there are plenty and well documented, but as I need only one single snippet, I would dislike to dive deep into documentations to seek for a way how to do it. If I am going to write a GUI program, I surely would do that, but this is needed only as a few lines for my ad hoc script.

What would be the easiest and the most straightforward way for me (GUI noob) to write in Python following piece of code? Less lines = more happiness.

  1. Grab a JPEG picture by filename.
  2. Display it's thumbnail.
  3. Below the thumbnail display a textfield so the user can type in a caption.
  4. Wait until user hits ENTER key on his/her keyboard. In that case, close and return the input.
  5. ...or wait until user hits DELETE key. In that case, close and return an information about the decision (to delete the picture).

Dependencies or Linux-only solutions are okay. I need to run this on Xubuntu machine. Any code snippets, please? I believe this is a matter of 5 minutes for someone skilled in Python GUI field. I would need to study loads of library docs. Thank you!

Below is a minimal python script that more or less fits the spec.

It requires python2 and pyqt4 packages to be installed, and it won't work with python3 (although it could quite easily be adapted to do so if necessary).

If the user types in a valid caption and presses enter, the script will return with status code 0 and print the caption to stdout; otherwise, if the user enters an invalid caption (empty or whitespace only), or simply closes the dialog without doing anything, the script will return with status code 1 and print nothing.

example bash usage :

$ CAPTION=$(python imgviewer.py image.jpg)
$ [ $? -eq 0 ] && echo $CAPTION 

imgviewer.py :

import sys, os
from PyQt4 import QtGui, QtCore

class Dialog(QtGui.QDialog):
    def __init__(self, path):
        QtGui.QDialog.__init__(self)
        self.viewer = QtGui.QLabel(self)
        self.viewer.setMinimumSize(QtCore.QSize(400, 400))
        self.viewer.setScaledContents(True)
        self.viewer.setPixmap(QtGui.QPixmap(path))
        self.editor = QtGui.QLineEdit(self)
        self.editor.returnPressed.connect(self.handleReturnPressed)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.viewer)
        layout.addWidget(self.editor)

    def handleReturnPressed(self):
        if self.editor.text().simplified().isEmpty():
            self.reject()
        else:
            self.accept()

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    args = app.arguments()[1:]
    if len(args) == 1:
        dialog = Dialog(args[0])
        if dialog.exec_() == QtGui.QDialog.Accepted:
            print dialog.editor.text().simplified().toLocal8Bit().data()
            sys.exit(0)
    else:
        print 'ERROR: wrong number of arguments'
    sys.exit(1)

There are several good GUI libraries for Python. The "standard" library that comes built-in with python is tkinter : http://wiki.python.org/moin/TkInter . Some says that wxPython is much more powerful and straightforward: http://www.wxpython.org/ .

I think that you can start with wxPython, they have many many tutorials and examples you can dig into (just run the DEMO).

They have an example called "ImageBrowser" which might be a very good starting point.

Regarding the communication between the different apps, you can use "pipes" and "redirections" to communicate. But if everything is written in python, I think this is the wrong way to go, you can show the image form within your python script and get the result internally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM