• pyQt: eg3


    import sys
    import urllib2
    from PyQt4 import QtCore
    from PyQt4 import QtGui
    
    class Form(QtGui.QDialog):
        def __init__(self, parent=None):
            super(Form, self).__init__(parent)
    
            date = self.getdata()
            rates = sorted(self.rates.keys())
    
            dateLabel = QtGui.QLabel(date)
            self.fromComboBox = QtGui.QComboBox()
            self.fromComboBox.addItems(rates)
            self.fromSpinBox = QtGui.QDoubleSpinBox()
            self.fromSpinBox.setRange(0.01, 10000000.00)
            self.fromSpinBox.setValue(1.00)
            self.toComboBox = QtGui.QComboBox()
            self.toComboBox.addItems(rates)
            self.toLabel = QtGui.QLabel("1.00")
    
            grid = QtGui.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,
                QtCore.SIGNAL("currentIndexChanged(int)"), self.updateUi)
            self.connect(self.toComboBox,
                QtCore.SIGNAL("currentIndexChanged(int)"), self.updateUi)
            self.connect(self.fromSpinBox,
                QtCore.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}".format(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:
    {0}".format(e)    
    
    app = QtGui.QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()
  • 相关阅读:
    【MYSQL】SQL 的join 区别
    【Django】Django model与数据库操作对应关系(转)
    【Mysql】复制表结构+数据(转)
    【Django】Python web开发:几个模板系统的性能对比(转)
    【Mysql】Mysql关键字
    【Mysql】MySQL与Oracle的大小写问题
    Linux常用操作
    执行程序的两种方式
    Django框架的安装与使用
    web介绍
  • 原文地址:https://www.cnblogs.com/lypy/p/6394363.html
Copyright © 2020-2023  润新知