2008-08-12

PyQt -- 匯率換算

PyQt同時兼具Python與Qt的優點--你可以隨時採用Python原本就具有的功能,也可以快速地描繪出一個你所需要的GUI視窗。接下來的匯率換算程式,是一個很好的範例:

#!/usr/bin/env python
# Copyright (c) 2007-8 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 2 of the License, or
# version 3 of the License, or (at your option) any later version. It is
# provided for educational purposes and is distributed in the hope that
# it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details.

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


class Form(QDialog):

def __init__(self, parent=None):
super(Form, self).__init__(parent)

date = self.getdata()
rates = sorted(self.rates.keys())

dateLabel = QLabel(date)
self.fromComboBox = QComboBox()
self.fromComboBox.addItems(rates)
self.fromSpinBox = QDoubleSpinBox()
self.fromSpinBox.setRange(0.01, 10000000.00)
self.fromSpinBox.setValue(1.00)
self.toComboBox = QComboBox()
self.toComboBox.addItems(rates)
self.toLabel = QLabel("1.00")
grid = QGridLayout()
grid.addWidget(dateLabel, 0, 0)
grid.addWidget(self.fromComboBox, 1, 0)
grid.addWidget(self.fromSpinBox, 1, 1)
grid.addWidget(self.toComboBox, 2, 0)
grid.addWidget(self.toLabel, 2, 1)
self.setLayout(grid)
self.connect(self.fromComboBox,
SIGNAL("currentIndexChanged(int)"), self.updateUi)
self.connect(self.toComboBox,
SIGNAL("currentIndexChanged(int)"), self.updateUi)
self.connect(self.fromSpinBox,
SIGNAL("valueChanged(double)"), self.updateUi)
self.setWindowTitle("Currency")


def updateUi(self):
to = unicode(self.toComboBox.currentText())
from_ = unicode(self.fromComboBox.currentText())
amount = (self.rates[from_] / self.rates[to]) * \
self.fromSpinBox.value()
self.toLabel.setText("%0.2f" % amount)


def getdata(self): # Idea taken from the Python Cookbook
self.rates = {}
try:
date = "Unknown"
fh = urllib2.urlopen("http://www.bankofcanada.ca"
"/en/markets/csv/exchange_eng.csv")
for line in fh:
line = line.rstrip()
if not line or line.startswith(("#", "Closing ")):
continue
fields = line.split(",")
if line.startswith("Date "):
date = fields[-1]
else:
try:
value = float(fields[-1])
self.rates[unicode(fields[0])] = value
except ValueError:
pass
return "Exchange Rates Date: " + date
except Exception, e:
return "Failed to download:\n%s" % e


app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

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

照例又是從load library開始, 不過這裡要特別注意有個比較特別的,就是import urllib2。它是一個用來呼叫跟internet有關函式的library. 而這回我們要利用裡面的函式到網路上去抓檔案。
 self.fromComboBox = QComboBox()
self.fromComboBox.addItems(rates)
self.fromSpinBox = QDoubleSpinBox()
self.fromSpinBox.setRange(0.01, 10000000.00)
self.fromSpinBox.setValue(1.00)
self.toComboBox = QComboBox()
self.toComboBox.addItems(rates)
self.toLabel = QLabel("1.00")

接下來照例還是宣告視窗的class. 這裡提供了兩個QComboBox跟一個QDoubleSpinBox,並且可以給定範圍與起始值。
grid = QGridLayout()
grid.addWidget(dateLabel, 0, 0)
grid.addWidget(self.fromComboBox, 1, 0)
grid.addWidget(self.fromSpinBox, 1, 1)
grid.addWidget(self.toComboBox, 2, 0)
grid.addWidget(self.toLabel, 2, 1)
self.setLayout(grid)

這裡是用來排列前面三個box的位置, 你可以看到0,0~2,1的數字,它就是像矩陣一樣,分別從0,0;0,1;1,0;1,1;2,0;2,1.但是因為沒有指定任何box給0,1所以那裡會是空的。接著就是定義連接的動作,之後就是自我定義的函式,把資料讀進來,並且放入rates這個dictionary並且排序等等動作,你可以看程式碼來理解。

沒有留言: