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 相关文章推荐
sqlalchemy对象转dict的示例
Apr 22 Python
Python中使用MELIAE分析程序内存占用实例
Feb 18 Python
Python算法之求n个节点不同二叉树个数
Oct 27 Python
详解Python使用tensorflow入门指南
Feb 09 Python
python 中字典嵌套列表的方法
Jul 03 Python
Django对数据库进行添加与更新的例子
Jul 12 Python
python实现树的深度优先遍历与广度优先遍历详解
Oct 26 Python
Python+OpenCV实现旋转文本校正方式
Jan 09 Python
pytorch中的inference使用实例
Feb 20 Python
opencv+pyQt5实现图片阈值编辑器/寻色块阈值利器
Nov 13 Python
使用tensorflow 实现反向传播求导
May 26 Python
关于python3 opencv 图像二值化的问题(cv2.adaptiveThreshold函数)
Apr 04 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
PHP的FTP学习(三)
2006/10/09 PHP
javascript 流畅动画实现原理
2009/09/08 Javascript
jQuery getJSON 处理json数据的代码
2010/07/26 Javascript
css值转换成数值请抛弃parseInt
2011/10/24 Javascript
javascript写的一个模拟阅读小说的程序
2014/04/04 Javascript
谷歌浏览器调试JavaScript小技巧
2014/12/29 Javascript
Map.vue基于百度地图组件重构笔记分享
2017/04/17 Javascript
微信小程序搜索组件wxSearch实例详解
2017/06/08 Javascript
vue页面使用阿里oss上传功能的实例(二)
2017/08/09 Javascript
最新Javascript程序员面试试题和解题方法
2017/11/23 Javascript
html中通过JS获取JSON数据并加载的方法
2017/11/30 Javascript
JS实现的字符串数组去重功能小结
2019/06/17 Javascript
React-redux实现小案例(todolist)的过程
2019/09/29 Javascript
[01:03:03]VP vs Mineski 2018国际邀请赛淘汰赛BO3 第一场 8.22
2018/08/23 DOTA
一则python3的简单爬虫代码
2014/05/26 Python
详解Python设计模式编程中观察者模式与策略模式的运用
2016/03/02 Python
python记录程序运行时间的三种方法
2017/07/14 Python
Python中return self的用法详解
2018/07/27 Python
pycharm修改界面主题颜色的方法
2019/01/17 Python
Django给admin添加Action的步骤详解
2019/05/01 Python
Gauss-Seidel迭代算法的Python实现详解
2019/06/29 Python
解决Python设置函数调用超时,进程卡住的问题
2019/08/08 Python
Pytorch 实现权重初始化
2019/12/31 Python
CSS3过渡transition效果实例介绍
2016/05/03 HTML / CSS
The Kooples美国官方网站:为情侣提供的法国当代时尚品牌
2019/01/03 全球购物
Koral官方网站:女性时尚运动服
2019/04/10 全球购物
CHRONEXT英国:您的首选奢华腕表目的地
2020/03/30 全球购物
信号量和自旋锁的区别?如何选择使用?
2015/09/08 面试题
软件专业毕业生个人自我鉴定
2014/04/17 职场文书
高中生评语大全
2014/04/25 职场文书
建筑安全标语
2014/06/07 职场文书
机电一体化应届生求职信
2014/08/09 职场文书
离婚案件原告代理词
2015/05/23 职场文书
2016年寒假社会实践活动心得体会
2015/10/09 职场文书
队名及霸气口号大全
2015/12/25 职场文书
JS一分钟在github+Jekyll的博客中添加访问量功能的实现
2021/04/03 Javascript