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 pickle类库介绍(对象序列化和反序列化)
Nov 21 Python
Python生成随机密码的方法
Jun 16 Python
Windows下PyCharm安装图文教程
Aug 27 Python
python-opencv 将连续图片写成视频格式的方法
Jan 08 Python
基于python实现微信好友数据分析(简单)
Feb 16 Python
python集合能干吗
Jul 19 Python
详解基于Scrapy的IP代理池搭建
Sep 29 Python
Ubuntu配置Pytorch on Graph (PoG)环境过程图解
Nov 19 Python
plt.figure()参数使用详解及运行演示
Jan 08 Python
用Python将库打包发布到pypi
Apr 13 Python
python 定义函数 返回值只取其中一个的实现
May 21 Python
通过Python把学姐照片做成拼图游戏
Feb 15 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 加密解密内部算法
2010/04/22 PHP
sae使用smarty模板的方法
2013/12/17 PHP
PHP中的Streams详细介绍
2014/11/12 PHP
PHP图像处理技术实例总结【绘图、水印、验证码、图像压缩】
2018/12/08 PHP
php fread函数使用方法总结
2019/05/28 PHP
PHP 对象继承原理与简单用法示例
2020/04/21 PHP
解析JavaScript中点号“.”的多义性
2013/12/02 Javascript
如何利用JS通过身份证号获取当事人的生日、年龄、性别
2016/01/22 Javascript
jQuery配合coin-slider插件制作幻灯片效果的流程解析
2016/05/13 Javascript
jQuery中text() val()和html()的区别实例详解
2016/06/28 Javascript
D3.js实现饼状图的方法详解
2016/09/21 Javascript
jQuery实现移动端手机商城购物车功能
2016/09/24 Javascript
有关suggest快速删除后仍然出现下拉列表的bug问题
2016/12/02 Javascript
js实现随机抽选效果、随机抽选红色球效果
2017/01/13 Javascript
js实现3D图片展示效果
2017/03/09 Javascript
无循环 JavaScript(map、reduce、filter和find)
2017/04/08 Javascript
jquery ajax加载数据前台渲染方式 不用for遍历的方法
2018/08/09 jQuery
[48:32]VGJ.T vs Fnatic 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
[41:08]TNC vs VG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
Python基于OpenCV实现视频的人脸检测
2018/01/23 Python
完美解决在oj中Python的循环输入问题
2018/06/25 Python
pytz格式化北京时间多出6分钟问题的解决方法
2019/06/21 Python
python垃圾回收机制(GC)原理解析
2019/12/30 Python
浅谈pytorch 模型 .pt, .pth, .pkl的区别及模型保存方式
2020/05/25 Python
python类共享变量操作
2020/09/03 Python
Jacques Lemans德国:奥地利钟表品牌
2019/12/26 全球购物
英国领先的在线高尔夫设备零售商:Golfgeardirect
2020/12/11 全球购物
美国在线家具网站:GDFStudio
2021/03/13 全球购物
管理心得体会
2013/12/28 职场文书
高中打架检讨书
2014/02/13 职场文书
酒店管理专业毕业生求职自荐信
2014/04/28 职场文书
校园文明标语
2014/06/13 职场文书
导游词开场白
2015/01/31 职场文书
网络管理员岗位职责
2015/02/12 职场文书
2016春季运动会通讯稿
2015/07/18 职场文书
mysql查找连续出现n次以上的数字
2022/05/11 MySQL