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 相关文章推荐
urllib2自定义opener详解
Feb 07 Python
Python通过select实现异步IO的方法
Jun 04 Python
python学习之第三方包安装方法(两种方法)
Jul 30 Python
Python面向对象编程基础解析(一)
Oct 26 Python
matplotlib绘图实例演示标记路径
Jan 23 Python
浅谈python requests 的put, post 请求参数的问题
Jan 02 Python
numpy下的flatten()函数用法详解
May 27 Python
python多线程并发及测试框架案例
Oct 15 Python
Python导入数值型Excel数据并生成矩阵操作
Jun 09 Python
python 如何使用find和find_all爬虫、找文本的实现
Oct 16 Python
Python爬虫自动化获取华图和粉笔网站的错题(推荐)
Jan 08 Python
python中opencv实现图片文本倾斜校正
Jun 11 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
Content-type 的说明
2006/10/09 PHP
PHP中=赋值操作符对不同数据类型的不同行为
2011/01/02 PHP
Yii2框架BootStrap样式的深入理解
2016/11/07 PHP
PHP实现基于栈的后缀表达式求值功能
2017/11/10 PHP
PHP使用PhpSpreadsheet操作Excel实例详解
2020/03/26 PHP
仅Firefox中链接A无法实现模拟点击以触发其默认行为
2011/07/31 Javascript
JS this作用域以及GET传输值过长的问题解决方法
2013/08/06 Javascript
javascript阻止scroll事件多次执行的思路及实现
2013/11/08 Javascript
Jquery操作radio的简单实例
2014/01/06 Javascript
node.js中的fs.mkdir方法使用说明
2014/12/17 Javascript
NodeJS学习笔记之Connect中间件模块(一)
2015/01/27 NodeJs
JQuery通过AJAX从后台获取信息显示在表格上并支持行选中
2015/09/15 Javascript
JavaScript实现的Tween算法及缓冲特效实例代码
2015/11/03 Javascript
学习使用grunt来打包JavaScript和CSS程序的教程
2016/01/04 Javascript
jQuery实现元素拖拽并cookie保存顺序的方法
2016/02/20 Javascript
浅谈Vue.js
2017/03/02 Javascript
jquery PrintArea 实现票据的套打功能(代码)
2017/03/17 Javascript
vue中进入详情页记住滚动位置的方法(keep-alive)
2018/09/21 Javascript
Vue中使用ElementUI使用第三方图标库iconfont的示例
2018/10/11 Javascript
详解vue-router导航守卫
2019/01/19 Javascript
详解jquery和vue对比
2019/04/16 jQuery
Vue 使用计时器实现跑马灯效果的实例代码
2019/07/11 Javascript
举例讲解Python中is和id的用法
2015/04/03 Python
Python实现文件内容批量追加的方法示例
2017/08/29 Python
python获取酷狗音乐top500的下载地址 MP3格式
2018/04/17 Python
PyQt5+requests实现车票查询工具
2019/01/21 Python
python3.6、opencv安装环境搭建过程(图文教程)
2019/11/05 Python
keras读取训练好的模型参数并把参数赋值给其它模型详解
2020/06/15 Python
python用Tkinter做自己的中文代码编辑器
2020/09/07 Python
详解HTML5通讯录获取指定多个人的信息
2016/12/20 HTML / CSS
比驿:全球酒店比价网
2018/06/20 全球购物
No7 Beauty美国官网:英国国民护肤品牌
2019/10/31 全球购物
abstract class和interface有什么区别
2013/08/04 面试题
中国好声音广告词
2014/03/18 职场文书
500字作文之关于爸爸
2019/11/14 职场文书
Python实现将多张图片合成MP4视频并加入背景音乐
2022/04/28 Python