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的Tornado框架的HTTP客户端的教程
Apr 24 Python
python通过socket查询whois的方法
Jul 18 Python
Python内置函数OCT详解
Nov 09 Python
PyQt5每天必学之创建窗口居中效果
Apr 19 Python
使用python装饰器计算函数运行时间的实例
Apr 21 Python
Python格式化日期时间操作示例
Jun 28 Python
Python装饰器模式定义与用法分析
Aug 06 Python
Python 实现遥感影像波段组合的示例代码
Aug 04 Python
使用python-Jenkins批量创建及修改jobs操作
May 12 Python
Django项目如何获得SSL证书与配置HTTPS
Apr 30 Python
Python中常见的导入方式总结
May 06 Python
详解Python flask的前后端交互
Mar 31 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 curl 并发最佳实践代码分享
2012/09/05 PHP
解析PHP缓存函数的使用说明
2013/05/10 PHP
浅析ThinkPHP中的pathinfo模式和URL重写
2014/01/06 PHP
Laravel5.7框架安装与使用学习笔记图文详解
2019/04/02 PHP
在laravel框架中实现封装公共方法全局调用
2019/10/14 PHP
Yii框架小部件(Widgets)用法实例详解
2020/05/15 PHP
纯JS实现的批量图片预览加载功能
2011/08/14 Javascript
jQuery添加/改变/移除CSS类及判断是否已经存在CSS
2014/08/20 Javascript
jQuery中:radio选择器用法实例
2015/01/03 Javascript
jQuery判断一个元素是否可见的方法
2015/06/05 Javascript
AngularJS 整理一些优化的小技巧
2016/08/18 Javascript
nodejs爬虫遇到的乱码问题汇总
2017/04/07 NodeJs
vue-router路由懒加载和权限控制详解
2017/12/13 Javascript
浅谈React深度编程之受控组件与非受控组件
2017/12/26 Javascript
JavaScript中Object基础内部方法图
2018/02/05 Javascript
WebSocket的通信过程与实现方法详解
2018/04/29 Javascript
vue-cli 打包使用history模式的后端配置实例
2018/09/20 Javascript
layer.js open 隐藏滚动条的例子
2019/09/05 Javascript
python实现微信跳一跳辅助工具步骤详解
2018/01/04 Python
selenium+python自动化测试之多窗口切换
2019/01/23 Python
用Python写一个模拟qq聊天小程序的代码实例
2019/03/06 Python
python实现登录密码重置简易操作代码
2019/08/14 Python
css3实现针线缝合效果(图解步骤)
2013/02/04 HTML / CSS
面向对象编程的优势是什么
2015/12/17 面试题
linux面试题参考答案(9)
2015/01/07 面试题
汉语言文学毕业生求职信
2013/10/01 职场文书
中学教师岗位职责
2013/11/26 职场文书
区域销售经理岗位职责
2013/12/10 职场文书
结婚周年感言
2014/02/24 职场文书
班主任新年寄语
2014/04/04 职场文书
2014公司党员自我评价范文
2014/09/11 职场文书
大学毕业晚会开场白
2015/05/29 职场文书
叶问观后感
2015/06/15 职场文书
三下乡活动心得体会
2016/01/23 职场文书
MySQL去除密码登录告警的方法
2022/04/20 MySQL
python的html标准库
2022/04/29 Python