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抓taobao图片爬虫
Oct 26 Python
python实现分析apache和nginx日志文件并输出访客ip列表的方法
Apr 04 Python
python实现将文本转换成语音的方法
May 28 Python
在Django的视图中使用form对象的方法
Jul 18 Python
Python中常见的数据类型小结
Aug 29 Python
Windows安装Python、pip、easy_install的方法
Mar 05 Python
Flask框架WTForm表单用法示例
Jul 20 Python
使用Python+wxpy 找出微信里把你删除的好友实例
Feb 21 Python
Windows下pycharm安装第三方库失败(通用解决方案)
Sep 17 Python
Django多数据库联用实现方法解析
Nov 12 Python
用Python远程登陆服务器的步骤
Apr 16 Python
Pytest之测试命名规则的使用
Apr 16 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生成图形(Libchart)实例
2013/11/06 PHP
php根据某字段对多维数组进行排序的方法
2015/03/07 PHP
ThinkPHP框架实现的邮箱激活功能示例
2018/06/15 PHP
Laravel多域名下字段验证的方法
2019/04/04 PHP
JavaScript 32位整型无符号操作示例
2013/12/08 Javascript
angularjs客户端实现压缩图片文件并上传实例
2015/07/06 Javascript
jQuery满意度星级评价插件特效代码分享
2015/08/19 Javascript
VUE利用vuex模拟实现新闻点赞功能实例
2017/06/28 Javascript
微信小程序wx.getImageInfo()如何获取图片信息
2018/01/26 Javascript
使用vue-cli3新建一个项目并写好基本配置(推荐)
2019/04/24 Javascript
Vue动态组件和异步组件原理详解
2019/05/06 Javascript
django js 实现表格动态标序号的实例代码
2019/07/12 Javascript
浅谈v-for 和 v-if 并用时筛选条件方法
2019/11/07 Javascript
vue 中使用print.js导出pdf操作
2020/11/13 Javascript
vue 组件基础知识总结
2021/01/26 Vue.js
[42:32]DOTA2上海特级锦标赛B组资格赛#2 Fnatic VS Spirit第二局
2016/02/27 DOTA
[01:08:48]LGD vs OG 2018国际邀请赛淘汰赛BO3 第三场 8.25
2018/08/29 DOTA
在Python的Django框架中编写编译函数
2015/07/20 Python
flask使用session保存登录状态及拦截未登录请求代码
2018/01/19 Python
在python中bool函数的取值方法
2018/11/01 Python
Django框架封装外部函数示例
2019/05/28 Python
django-rest-framework 自定义swagger过程详解
2019/07/18 Python
PyCharm 2020.2.2 x64 下载并安装的详细教程
2020/10/15 Python
pytest fixtures装饰器的使用和如何控制用例的执行顺序
2021/01/28 Python
CSS3 实现发光边框特效
2020/11/11 HTML / CSS
HTML5中的autofocus(自动聚焦)属性介绍
2014/04/23 HTML / CSS
世界汽车零件:World Car Parts
2019/09/04 全球购物
新品发布会策划方案
2014/06/08 职场文书
销售行政专员岗位职责
2014/06/10 职场文书
法学专业毕业生求职信
2014/06/12 职场文书
车辆年检委托书范本
2014/10/14 职场文书
学习心理学的体会
2014/11/07 职场文书
2014年学校工作总结
2014/11/20 职场文书
教师求职自荐信范文
2015/03/04 职场文书
民事答辩状范本
2015/05/21 职场文书
用人单位的规章制度,怎样制定才是有效的?
2019/07/09 职场文书