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使用urllib模块开发的多线程豆瓣小站mp3下载器
Jan 16 Python
解析Python中的__getitem__专有方法
Jun 27 Python
关于Python正则表达式 findall函数问题详解
Mar 22 Python
Django中的ajax请求
Oct 19 Python
解决nohup执行python程序log文件写入不及时的问题
Jan 14 Python
对python字典过滤条件的实例详解
Jan 22 Python
浅谈PYTHON 关于文件的操作
Mar 19 Python
tensorflow的ckpt及pb模型持久化方式及转化详解
Feb 12 Python
Python爬虫爬取、解析数据操作示例
Mar 27 Python
PyInstaller运行原理及常用操作详解
Jun 13 Python
Python 利用OpenCV给照片换底色的示例代码
Aug 03 Python
总结Pyinstaller的坑及终极解决方法(小结)
Sep 21 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
php5.3提示Function ereg() is deprecated Error问题解决方法
2014/11/12 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
弹出广告特效代码(一个IP只弹出一次)
2007/05/11 Javascript
MooTools 1.2介绍
2009/09/14 Javascript
让你的网站可编辑的实现js代码
2009/10/19 Javascript
动态的改变IFrame的高度实现IFrame自动伸展适应高度
2012/12/28 Javascript
js的hasownproperty使用示例
2014/03/02 Javascript
手机号码,密码正则验证
2014/09/04 Javascript
javascript for-in有序遍历json数据并探讨各个浏览器差异
2015/11/30 Javascript
简单对比分析JavaScript中的apply,call与this的使用
2015/12/04 Javascript
JS实现获取剪贴板内容的方法
2016/06/21 Javascript
微信小程序开发之录音机 音频播放 动画实例 (真机可用)
2016/12/08 Javascript
详解React Native 采用Fetch方式发送跨域POST请求
2017/11/15 Javascript
node app 打包工具pkg的具体使用
2019/01/17 Javascript
浅析vue中的MVVM实现原理
2019/03/04 Javascript
使用JavaScript获取扫码枪扫描得到的条形码的思路代码详解
2020/06/10 Javascript
vue移动端写的拖拽功能示例代码
2020/09/09 Javascript
vuex刷新后数据丢失的解决方法
2020/10/18 Javascript
[02:42]岂曰无衣,与子同袍!DOTA2致敬每一位守护人
2020/02/17 DOTA
Python 调用DLL操作抄表机
2009/01/12 Python
在Python中操作文件之truncate()方法的使用教程
2015/05/25 Python
Python爬虫实例爬取网站搞笑段子
2017/11/08 Python
python登录并爬取淘宝信息代码示例
2017/12/09 Python
pip install urllib2不能安装的解决方法
2018/06/12 Python
使用TensorFlow实现SVM
2018/09/06 Python
Python实现图片转字符画的代码实例
2019/02/22 Python
python3中类的继承以及self和super的区别详解
2019/06/26 Python
美国知名保健品网站:LuckyVitamin(支持中文)
2017/08/09 全球购物
联想智利官方网站:Lenovo Chile
2020/06/03 全球购物
优秀毕业生求职推荐信范文
2013/11/21 职场文书
民生工作实施方案
2014/05/31 职场文书
美术社团活动总结
2014/06/27 职场文书
订货会邀请函
2015/01/31 职场文书
2015年语文教研组工作总结
2015/05/23 职场文书
看看如何用Python绘制小米新版天价logo
2021/04/20 Python
MySQL 数据丢失排查案例
2021/05/08 MySQL