调用的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,]*") 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 汇率计算的资料请关注其它相关文章!
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月23日
2024年11月23日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]