python 实现一个图形界面的汇率计算器
作者:yxs 时间:2021-05-07 07:12:24
调用的api接口:
https://api.exchangerate-api.com/v4/latest/USD
完整代码
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()
运行效果:
来源:https://data-flair.training/blogs/currency-converter-python/
标签:python,图形界面,汇率计算
0
投稿
猜你喜欢
在Python的gevent框架下执行异步的Solr查询的教程
2022-12-29 11:26:49
一文讲清base64编码原理
2023-04-10 23:51:48
Pytest 自动化测试框架的使用
2022-11-12 16:47:46
Pycharm取消py脚本中SQL识别的方法
2023-10-30 16:23:13
用IE浏览器UTF-8页面是一片空白
2009-06-14 19:55:00
GoLang中Strconv库有哪些常用方法
2024-05-09 10:11:17
python读写修改Excel之xlrd&xlwt&xlutils
2022-04-03 16:35:43
python模拟点击在ios中实现的实例讲解
2021-11-28 13:03:34
解决Pytorch训练过程中loss不下降的问题
2023-03-01 09:30:22
golang中defer的关键特性示例详解
2023-08-06 06:12:45
CSS制作圆角导航的另一思路
2008-11-06 11:39:00
Go基础教程系列之defer、panic和recover详解
2024-02-02 06:34:11
Python实现拼音转换
2023-03-28 06:25:34
Python连接Mysql实现图书借阅系统
2024-01-17 12:08:26
face_recognition库在python的安装
2021-06-16 02:29:27
Python 限制线程的最大数量的方法(Semaphore)
2022-03-02 06:24:09
python打包成so文件过程解析
2022-03-19 18:08:46
Python接口自动化之浅析requests模块get请求
2021-12-07 07:20:45
Mysql5.7如何修改root密码
2024-01-26 21:22:28
Pytorch maxpool的ceil_mode用法
2023-03-20 13:28:05