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遍历类中所有成员的方法
Mar 18 Python
python连接远程ftp服务器并列出目录下文件的方法
Apr 01 Python
详解Python中break语句的用法
May 14 Python
利用selenium 3.7和python3添加cookie模拟登陆的实现
Nov 20 Python
python实现k-means聚类算法
Feb 23 Python
python 异或加密字符串的实例
Oct 14 Python
在Python 中同一个类两个函数间变量的调用方法
Jan 31 Python
python 实现GUI(图形用户界面)编程详解
Jul 17 Python
详解python中的index函数用法
Aug 06 Python
Pytorch技巧:DataLoader的collate_fn参数使用详解
Jan 08 Python
pytorch:model.train和model.eval用法及区别详解
Feb 20 Python
基于Python的图像阈值化分割(迭代法)
Nov 20 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中PDO方式实现数据库的增删改查
2015/05/17 PHP
php 数组处理函数extract详解及实例代码
2016/11/23 PHP
thinkphp关于简单的权限判定方法
2017/04/03 PHP
PHP自动载入类文件函数__autoload的使用方法
2019/03/25 PHP
JQuery的一些小应用收集
2010/03/27 Javascript
jquery弹出关闭遮罩层实例
2013/08/06 Javascript
jquery实现弹出层效果实例
2015/05/19 Javascript
理解Javascript的动态语言特性
2015/06/17 Javascript
jquery 实现输入邮箱时自动补全下拉提示功能
2015/10/04 Javascript
基于vue.js实现图片轮播效果
2016/12/01 Javascript
Jquery Easyui分割按钮组件SplitButton使用详解(17)
2016/12/18 Javascript
JavaScript 反射和属性赋值实例解析
2019/10/28 Javascript
微信小程序地图绘制线段并且测量(实例代码)
2020/01/02 Javascript
在vue项目中引用Antv G2,以饼图为例讲解
2020/10/28 Javascript
[01:04:09]DOTA2-DPC中国联赛 正赛 iG vs VG BO3 第二场 2月2日
2021/03/11 DOTA
Python使用正则匹配实现抓图代码分享
2015/04/02 Python
Python序列化基础知识(json/pickle)
2017/10/19 Python
python3+PyQt5重新实现QT事件处理程序
2018/04/19 Python
Django 批量插入数据的实现方法
2020/01/12 Python
用Python在Excel里画出蒙娜丽莎的方法示例
2020/04/28 Python
Python的信号库Blinker用法详解
2020/12/31 Python
社区党务公开实施方案
2014/03/18 职场文书
学校三节实施方案
2014/06/09 职场文书
学雷锋志愿者活动方案
2014/08/21 职场文书
基层党员四风问题自我剖析材料
2014/09/29 职场文书
2014年销售人员工作总结
2014/11/27 职场文书
高三毕业评语
2014/12/31 职场文书
2016春节家属慰问信
2015/03/25 职场文书
2015年药店店长工作总结
2015/04/29 职场文书
员工加薪申请报告
2015/05/15 职场文书
2016年度员工工作表现评语
2015/12/02 职场文书
2016年“5.12”国际护士节活动总结
2016/04/06 职场文书
python代码实现扫码关注公众号登录的实战
2021/11/01 Python
Spring Data JPA框架持久化存储数据到数据库
2022/04/28 Java/Android
MySQL 数据表操作
2022/05/04 MySQL
Windows server 2016服务器基本设置
2022/08/14 Servers