python 實(shí)現(xiàn)一個(gè)圖形界面的匯率計(jì)算器
更新時(shí)間:2020年11月09日 11:40:07 投稿:yxs
這篇文章主要介紹了python 實(shí)現(xiàn)一個(gè)圖形界面的匯率計(jì)算器,幫助大家更好的理解和學(xué)習(xí)如何制作gui程序,感興趣的朋友可以了解下
調(diào)用的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()
運(yùn)行效果:

以上就是python 實(shí)現(xiàn)一個(gè)圖形界面的匯率計(jì)算器的詳細(xì)內(nèi)容,更多關(guān)于python 匯率計(jì)算的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實(shí)現(xiàn)復(fù)制大量文件功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)復(fù)制大量文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Python虛擬環(huán)境venv實(shí)戰(zhàn)過(guò)程詳解
Python的虛擬環(huán)境可以幫助我們?cè)谕慌_(tái)機(jī)器上,同時(shí)使用不同的Python版本和庫(kù),方便管理和開(kāi)發(fā),下面這篇文章主要給大家介紹了關(guān)于Python虛擬環(huán)境venv的相關(guān)資料,需要的朋友可以參考下2023-06-06
python 對(duì)字典按照value進(jìn)行排序的方法
這篇文章主要介紹了python 對(duì)字典按照value進(jìn)行排序的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python實(shí)現(xiàn)清理重復(fù)文件功能的示例代碼
在電腦上或多或少的存在一些重復(fù)文件,體積小的倒沒(méi)什么,如果體積大的就很占內(nèi)存了。本文用python制作了一個(gè)刪除重復(fù)文件的小工具,核心代碼很簡(jiǎn)單,希望對(duì)你有所幫助2022-07-07

