Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Started with PyQt

Tags:

python

qt

pyqt

I'm testing out some of the examples in Rapid GUI Programming with Python and Qt, but running into a stumbling block here or where. When I copied to following exercise (verbatim, from the book):

import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QApplication(sys.argv)

try:
    due = QTime.currentTime()
    message = "Alert!"

    if len(sys.argv) < 2:
        raise ValueError

    hours, mins = sys.argv[1].split(":")
    due = QTime(int(hours), int(mins))

    if not due.isValid():
        raise ValueError
    if len(sys.argv) > 2:
        message = " ".join(sys.argv[2:])

except ValueError:
    message = "Usage: alert.pyw HH:MM [optional message*]" # 24hr Clock

while QTime.currentTime() < due:
    time.sleep(20) # 20 seconds

label = QLabel("<font color=red size=72><b>" + message + "</b></font>")

label.setWindowFlags(Qt.SplashScreen)
label.show()

QTimer.singleShot(60000, app.quit) # 1 minute

app.exec_()

I get the following error:

andy@ASUSix:~/Documents/Programming/Python/PyQt$ from: can't read /var/mail/PyQt4.QtCore
from: can't read /var/mail/PyQt4.QtGui
./alert.pyw: line 6: syntax error near unexpected token `('
./alert.pyw: line 6: `app = QApplication(sys.argv)

What's going wrong here? Is my PATH set up incorrectly?

like image 539
Oso Avatar asked Dec 29 '22 07:12

Oso


1 Answers

You probably forgot to add a shebang to your script, to tell your shell to actually run it with the Python interpreter. Try adding

#!/usr/bin/python

as the first line in your script, provided that's where your Python interpreter is installed. You might want to try

which python

in case you're not sure.

like image 84
Jim Brissom Avatar answered Jan 13 '23 00:01

Jim Brissom