python课程设计计算器_python 实现一个图形界面的汇率计算器

调用的api接口:

b_0_202011091912299832.png

完整代码

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()

运行效果:

b_0_202011091912291020.png

以上就是python 实现一个图形界面的汇率计算器的详细内容,更多关于python 汇率计算的资料请关注萬仟网其它相关文章!

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值