python 实现一个图形界面的汇率计算器


Posted in Python onNovember 09, 2020

调用的api接口:

https://api.exchangerate-api.com/v4/latest/USD

python 实现一个图形界面的汇率计算器

完整代码

import requests
from tkinter import *
import tkinter as tk
from tkinter import ttk


class RealTimeCurrencyConverter():
  def __init__(self,url):
      self.data = requests.get(url).json()
      self.currencies = self.data['rates']

  def convert(self, from_currency, to_currency, amount): 
    initial_amount = amount 
    if from_currency != 'USD' : 
      amount = amount / self.currencies[from_currency] 
 
    
    amount = round(amount * self.currencies[to_currency], 4) 
    return amount

class App(tk.Tk):

  def __init__(self, converter):
    tk.Tk.__init__(self)
    self.title = 'Currency Converter'
    self.currency_converter = converter

    
    self.geometry("500x200")
    
    
    self.intro_label = Label(self, text = 'Welcome to Real Time Currency Convertor', fg = 'blue', relief = tk.RAISED, borderwidth = 3)
    self.intro_label.config(font = ('Courier',15,'bold'))

    self.date_label = Label(self, text = f"1 Indian Rupee equals = {self.currency_converter.convert('INR','USD',1)} USD \n Date : {self.currency_converter.data['date']}", relief = tk.GROOVE, borderwidth = 5)

    self.intro_label.place(x = 10 , y = 5)
    self.date_label.place(x = 160, y= 50)

    
    valid = (self.register(self.restrictNumberOnly), '%d', '%P')
    self.amount_field = Entry(self,bd = 3, relief = tk.RIDGE, justify = tk.CENTER,validate='key', validatecommand=valid)
    self.converted_amount_field_label = Label(self, text = '', fg = 'black', bg = 'white', relief = tk.RIDGE, justify = tk.CENTER, width = 17, borderwidth = 3)

    
    self.from_currency_variable = StringVar(self)
    self.from_currency_variable.set("INR") 
    self.to_currency_variable = StringVar(self)
    self.to_currency_variable.set("USD") 

    font = ("Courier", 12, "bold")
    self.option_add('*TCombobox*Listbox.font', font)
    self.from_currency_dropdown = ttk.Combobox(self, textvariable=self.from_currency_variable,values=list(self.currency_converter.currencies.keys()), font = font, state = 'readonly', width = 12, justify = tk.CENTER)
    self.to_currency_dropdown = ttk.Combobox(self, textvariable=self.to_currency_variable,values=list(self.currency_converter.currencies.keys()), font = font, state = 'readonly', width = 12, justify = tk.CENTER)

    
    self.from_currency_dropdown.place(x = 30, y= 120)
    self.amount_field.place(x = 36, y = 150)
    self.to_currency_dropdown.place(x = 340, y= 120)
    
    self.converted_amount_field_label.place(x = 346, y = 150)
    
    
    self.convert_button = Button(self, text = "Convert", fg = "black", command = self.perform) 
    self.convert_button.config(font=('Courier', 10, 'bold'))
    self.convert_button.place(x = 225, y = 135)

  def perform(self):
    amount = float(self.amount_field.get())
    from_curr = self.from_currency_variable.get()
    to_curr = self.to_currency_variable.get()

    converted_amount = self.currency_converter.convert(from_curr,to_curr,amount)
    converted_amount = round(converted_amount, 2)

    self.converted_amount_field_label.config(text = str(converted_amount))
  
  def restrictNumberOnly(self, action, string):
    regex = re.compile(r"[0-9,]*?(\.)?[0-9,]*$")
    result = regex.match(string)
    return (string == "" or (string.count('.') <= 1 and result is not None))

if __name__ == '__main__':
  url = 'https://api.exchangerate-api.com/v4/latest/USD'
  converter = RealTimeCurrencyConverter(url)

  App(converter)
  mainloop()

运行效果:

python 实现一个图形界面的汇率计算器

以上就是python 实现一个图形界面的汇率计算器的详细内容,更多关于python 汇率计算的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
用python写asp详细讲解
Dec 16 Python
Python程序设计入门(2)变量类型简介
Jun 16 Python
python清理子进程机制剖析
Nov 23 Python
dataframe 按条件替换某一列中的值方法
Jan 29 Python
python实现二维数组的对角线遍历
Mar 02 Python
使用Python Pandas处理亿级数据的方法
Jun 24 Python
python获取Pandas列名的几种方法
Aug 07 Python
python根据文本生成词云图代码实例
Nov 15 Python
python__new__内置静态方法使用解析
Jan 07 Python
无惧面试,带你搞懂python 装饰器
Aug 17 Python
Python编写单元测试代码实例
Sep 10 Python
90行Python代码开发个人云盘应用
Apr 20 Python
python 读取串口数据的示例
Nov 09 #Python
Cpython解释器中的GIL全局解释器锁
Nov 09 #Python
OpenCV实现机器人对物体进行移动跟随的方法实例
Nov 09 #Python
基于python爬取梨视频实现过程解析
Nov 09 #Python
Python eval函数介绍及用法
Nov 09 #Python
python tkinter的消息框模块(messagebox,simpledialog)
Nov 07 #Python
python 用struct模块解决黏包问题
Nov 07 #Python
You might like
推荐几款用 Sublime Text 开发 Laravel 所用到的插件
2014/10/30 PHP
smarty实现多级分类的方法
2014/12/05 PHP
JavaScript接口实现代码 (Interfaces In JavaScript)
2010/06/11 Javascript
JavaScript打开word文档的实现代码(c#)
2012/04/16 Javascript
jquery插件之信息弹出框showInfoDialog(成功/错误/警告/通知/背景遮罩)
2013/01/09 Javascript
一个js控制的导航菜单实例代码
2013/12/03 Javascript
jQuery使用$.ajax进行即时验证实例详解
2015/12/11 Javascript
JavaScript+html5 canvas制作色彩斑斓的正方形效果
2016/01/27 Javascript
jQuery实现简单的DIV拖动效果
2016/02/19 Javascript
js和jQuery设置Opacity半透明 兼容IE6
2016/05/24 Javascript
浅析JSONP技术原理及实现
2016/06/08 Javascript
使用js获取地址栏参数的方法推荐(超级简单)
2016/06/14 Javascript
Node.js中看JavaScript的引用
2017/04/22 Javascript
JS设计模式之状态模式概念与用法分析
2018/02/05 Javascript
React key值的作用和使用详解
2018/08/23 Javascript
微信小程序实现图片滚动效果示例
2018/12/05 Javascript
微信域名检测接口调用演示步骤(含PHP、Python)
2019/12/08 Javascript
[01:46]DOTA2上海特锦赛小组赛英文解说KotlGuy采访
2016/02/27 DOTA
pymssql ntext字段调用问题解决方法
2008/12/17 Python
python实现朴素贝叶斯分类器
2018/03/28 Python
python遍历小写英文字母的方法
2019/01/02 Python
Python使用folium excel绘制point
2019/01/03 Python
python实现石头剪刀布程序
2021/01/20 Python
python调用自定义函数的实例操作
2019/06/26 Python
OpenCV 模板匹配
2019/07/10 Python
python关于倒排列的知识点总结
2020/10/13 Python
python遍历路径破解表单的示例
2020/11/21 Python
HTML5进度条特效
2014/12/18 HTML / CSS
澳大利亚最受欢迎的美发和美容在线商店:Catwalk
2018/12/12 全球购物
销售类个人求职信范文
2013/09/25 职场文书
教师自我鉴定范文
2013/11/10 职场文书
好的演讲稿开场白
2013/12/30 职场文书
1亿有多大教学反思
2014/05/01 职场文书
无犯罪记录证明范本
2014/09/15 职场文书
村主任“四风”问题个人对照检查材料思想汇报
2014/10/02 职场文书
建党伟业的观后感
2015/06/01 职场文书