Skip to content

Commit fc1d0de

Browse files
author
Deusdies
committed
Adding the first code files
0 parents  commit fc1d0de

9 files changed

+595
-0
lines changed

01-firstTutorial-alarm.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
from PySide.QtCore import *
3+
from PySide.QtGui import *
4+
import time
5+
6+
7+
app = QApplication(sys.argv)
8+
9+
10+
try:
11+
due = QTime.currentTime()
12+
message = "Alert!"
13+
14+
if len(sys.argv) < 2:
15+
raise ValueError
16+
17+
hours, minutes = sys.argv[1].split(":")
18+
due = QTime(int(hours), int(minutes))
19+
20+
if not due.isValid():
21+
raise ValueError
22+
23+
if len(sys.argv) > 2:
24+
message = " ".join(sys.argv[2:])
25+
26+
except ValueError:
27+
message = "Usage: firstTutorial-alarm.py HH:MM [optional message]" #24 hour clock
28+
29+
while QTime.currentTime() < due:
30+
time.sleep(10)
31+
32+
33+
34+
label = QLabel("<font color=red size=72><b>" + message + "</b></font>")
35+
label.setWindowFlags(Qt.SplashScreen)
36+
label.show()
37+
38+
QTimer.singleShot(20000, app.quit) #20 seconds
39+
app.exec_()
40+
41+
42+
43+
44+
45+
46+
47+
48+

02-secondTutorial-calculator.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
from PySide.QtCore import *
3+
from PySide.QtGui import *
4+
from math import *
5+
6+
7+
class Form(QDialog):
8+
9+
def __init__(self, parent=None):
10+
super(Form, self).__init__(parent)
11+
12+
self.browser = QTextBrowser()
13+
self.lineedit = QLineEdit("Type an expression and press Enter")
14+
self.lineedit.selectAll()
15+
16+
layout = QVBoxLayout()
17+
layout.addWidget(self.browser)
18+
layout.addWidget(self.lineedit)
19+
self.setLayout(layout)
20+
21+
self.lineedit.setFocus()
22+
23+
self.connect(self.lineedit, SIGNAL("returnPressed()"), self.updateUi)
24+
self.setWindowTitle("Calculate")
25+
26+
27+
def updateUi(self):
28+
try:
29+
text = self.lineedit.text()
30+
self.browser.append("%s = <b>%s</b>" % (text, eval(text)))
31+
except:
32+
self.browser.append("<font color=red>%s is invalid</font>" % text)
33+
34+
35+
36+
app = QApplication(sys.argv)
37+
form = Form()
38+
form.show()
39+
app.exec_()
40+
41+
42+
43+
44+
45+
46+
47+

03-thirdTutorial-currency.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import sys
2+
from PySide.QtCore import *
3+
from PySide.QtGui import *
4+
5+
import urllib2
6+
7+
8+
class Form(QDialog):
9+
10+
def __init__(self, parent=None):
11+
super(Form, self).__init__(parent)
12+
13+
date = self.getdata()
14+
rates = sorted(self.rates.keys())
15+
16+
dateLabel = QLabel(date)
17+
self.fromComboBox = QComboBox()
18+
self.fromComboBox.addItems(rates)
19+
self.fromSpinBox = QDoubleSpinBox()
20+
self.fromSpinBox.setRange(0.01, 10000000.00)
21+
self.fromSpinBox.setValue(1.00)
22+
self.toComboBox = QComboBox()
23+
self.toComboBox.addItems(rates)
24+
self.toLabel = QLabel("1.00")
25+
26+
grid = QGridLayout()
27+
grid.addWidget(dateLabel, 0, 0)
28+
grid.addWidget(self.fromComboBox, 1, 0)
29+
grid.addWidget(self.fromSpinBox, 1, 1)
30+
grid.addWidget(self.toComboBox, 2, 0)
31+
grid.addWidget(self.toLabel, 2, 1)
32+
self.setLayout(grid)
33+
34+
35+
self.connect(self.fromComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi)
36+
self.connect(self.toComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi)
37+
self.connect(self.fromSpinBox, SIGNAL("valueChanged(double)"), self.updateUi)
38+
39+
40+
def updateUi(self):
41+
to = self.toComboBox.currentText()
42+
from_ = self.fromComboBox.currentText()
43+
44+
amount = (self.rates[from_] / self.rates[to]) * self.fromSpinBox.value()
45+
self.toLabel.setText("%0.2f" % amount)
46+
47+
def getdata(self):
48+
self.rates = {}
49+
50+
try:
51+
date = "Unknown"
52+
53+
fh = urllib2.urlopen("http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv")
54+
55+
for line in fh:
56+
line = line.rstrip()
57+
if not line or line.startswith(("#", "Closing")):
58+
continue
59+
60+
fields = line.split(",")
61+
if line.startswith("Date "):
62+
date = fields[-1]
63+
64+
else:
65+
try:
66+
value = float(fields[-1])
67+
self.rates[fields[0]] = value
68+
except ValueError:
69+
pass
70+
71+
return "Exchange rates date: " + date
72+
except Exception, e:
73+
return "Failued to download:\n%s" % e
74+
75+
76+
app = QApplication(sys.argv)
77+
form = Form()
78+
form.show()
79+
app.exec_()
80+
81+
82+
83+

04-fourthTutorial-signals.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
from PySide.QtCore import *
3+
from PySide.QtGui import *
4+
5+
class Form(QDialog):
6+
7+
def __init__(self, parent=None):
8+
super(Form, self).__init__(parent)
9+
10+
dial = QDial()
11+
dial.setNotchesVisible(True)
12+
13+
spinbox = QSpinBox()
14+
15+
layout = QHBoxLayout()
16+
layout.addWidget(dial)
17+
layout.addWidget(spinbox)
18+
self.setLayout(layout)
19+
20+
self.connect(dial, SIGNAL("valueChanged(int)"), spinbox.setValue)
21+
self.connect(spinbox, SIGNAL("valueChanged(int)"), dial.setValue)
22+
23+
self.setWindowTitle("Signals and Slots")
24+
25+
26+
app = QApplication(sys.argv)
27+
form = Form()
28+
form.show()
29+
app.exec_()

06-sixthTutorial-moresignals.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import sys
2+
from PySide.QtCore import *
3+
from PySide.QtGui import *
4+
5+
class ZeroSpinBox(QSpinBox):
6+
7+
zeros = 0
8+
9+
def __init__(self, parent=None):
10+
super(ZeroSpinBox, self).__init__(parent)
11+
12+
self.connect(self, SIGNAL("valueChanged(int)"), self.checkzero)
13+
14+
def checkzero(self):
15+
if self.value() == 0:
16+
self.zeros += 1
17+
self.nulls = 5
18+
self.emit(SIGNAL("atzero"), self.zeros, self.nulls)
19+
20+
21+
22+
class Form(QDialog):
23+
24+
def __init__(self, parent=None):
25+
super(Form, self).__init__(parent)
26+
27+
dial = QDial()
28+
dial.setNotchesVisible(True)
29+
30+
zerospinbox = ZeroSpinBox()
31+
32+
layout = QHBoxLayout()
33+
layout.addWidget(dial)
34+
layout.addWidget(zerospinbox)
35+
self.setLayout(layout)
36+
37+
self.connect(dial, SIGNAL("valueChanged(int)"), zerospinbox.setValue)
38+
self.connect(zerospinbox, SIGNAL("valueChanged(int)"), dial.setValue)
39+
self.connect(zerospinbox, SIGNAL("atzero"), self.announce)
40+
41+
self.setWindowTitle("Signals and Slots")
42+
43+
def announce(self, zeros, nulls):
44+
print "ZeroSPinBox has been at zero " + str(zeros) + " times."
45+
print "The constant nulls is ", str(nulls)
46+
47+
48+
49+
app = QApplication(sys.argv)
50+
form = Form()
51+
form.show()
52+
app.exec_()
53+
54+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from PySide.QtGui import *
2+
from PySide.QtCore import *
3+
import sys
4+
5+
__appname__ = "Eight Video"
6+
7+
class Program(QDialog):
8+
9+
def __init__(self, parent=None):
10+
super(Program, self).__init__(parent)
11+
12+
openButton = QPushButton("Open")
13+
saveButton = QPushButton("Save")
14+
dirButton = QPushButton("Other")
15+
closeButton = QPushButton("Close...")
16+
17+
self.connect(openButton, SIGNAL("clicked()"), self.open)
18+
self.connect(saveButton, SIGNAL("clicked()"), self.save)
19+
20+
layout = QVBoxLayout()
21+
layout.addWidget(openButton)
22+
layout.addWidget(saveButton)
23+
layout.addWidget(dirButton)
24+
layout.addWidget(closeButton)
25+
self.setLayout(layout)
26+
27+
def open(self):
28+
29+
dir = "."
30+
31+
fileObj = QFileDialog.getOpenFileName(self, __appname__ + " Open File Dialog", dir=dir, filter="Text files (*.txt)")
32+
print fileObj
33+
print type(fileObj)
34+
35+
fileName = fileObj[0]
36+
37+
file = open(fileName, "r")
38+
read = file.read()
39+
file.close()
40+
print read
41+
42+
def save(self):
43+
dir = "."
44+
45+
fileObj = QFileDialog.getSaveFileName(self, __appname__, dir=dir, filter="Text Files (*.txt)")
46+
47+
print fileObj
48+
print type(fileObj)
49+
50+
contents = "Hello from http://py.bo.vc"
51+
52+
fileName = fileObj[0]
53+
54+
open(fileName, mode="w").write(contents)
55+
56+
57+
app = QApplication(sys.argv)
58+
form = Program()
59+
form.show()
60+
app.exec_()

0 commit comments

Comments
 (0)