python 实现一个图形界面的汇率计算器

作者:yxs 时间:2021-05-07 07:12:24 

调用的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 实现一个图形界面的汇率计算器

来源:https://data-flair.training/blogs/currency-converter-python/

标签:python,图形界面,汇率计算
0
投稿

猜你喜欢

  • python实现读取excel表格详解方法

    2023-03-12 04:48:37
  • Python+Flask编写一个简单的行人检测API

    2023-09-26 17:55:19
  • asp生成UTF-8格式的文件方法

    2008-01-26 20:59:00
  • phpMyAdmin开发人员访谈——4个人支持整个项目

    2010-05-26 15:34:00
  • Python中的进程操作模块(multiprocess.process)

    2022-09-17 23:10:32
  • 用PHP实现标准的IP Whois查询

    2023-11-14 19:35:01
  • 三种不同方式连接MySQL数据库的方法及示例

    2010-06-11 13:37:00
  • Oracle入侵常用操作命令整理

    2009-03-04 11:11:00
  • SqlServer 基础知识 数据检索、查询排序语句

    2011-11-03 16:46:12
  • 使用AJAX技术的十大理由

    2008-04-30 13:21:00
  • pycharm 批量修改变量名称的方法

    2021-09-07 22:48:29
  • css样式命名规则

    2008-04-30 12:31:00
  • ASP函数大全(数字函数)

    2009-06-01 12:33:00
  • 详解Python+OpenCV实现图像二值化

    2022-01-02 13:08:07
  • asp中用insert into语句向数据库插入记录(添加信息)的方法

    2011-02-05 10:46:00
  • 如何更改mysql命令下提示信息

    2010-10-25 19:48:00
  • 模拟实现 Range 的 insertNode() 方法

    2010-11-30 21:39:00
  • asp使用 sql_dmo 给表添加索引

    2010-03-17 20:57:00
  • python GUI库图形界面开发之PyQt5 Qt Designer工具(Qt设计师)详细使用方法及Designer ui文件转py文件方法

    2023-05-17 00:32:46
  • 在Python中调用ggplot的三种方法

    2023-08-23 00:40:58
  • asp之家 网络编程 m.aspxhome.com