python 實現(xiàn)一個圖形界面的匯率計算器
更新時間:2020年11月09日 11:40:07 投稿:yxs
這篇文章主要介紹了python 實現(xiàn)一個圖形界面的匯率計算器,幫助大家更好的理解和學習如何制作gui程序,感興趣的朋友可以了解下
調用的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()
運行效果:
以上就是python 實現(xiàn)一個圖形界面的匯率計算器的詳細內容,更多關于python 匯率計算的資料請關注腳本之家其它相關文章!
相關文章
Python字符串的encode與decode研究心得亂碼問題解決方法
為什么Python使用過程中會出現(xiàn)各式各樣的亂碼問題,明明是中文字符卻顯示成“\xe4\xb8\xad\xe6\x96\x87”的形式?2009-03-03Python中Parsel的兩種數(shù)據(jù)提取方式詳解
在網(wǎng)絡爬蟲的世界中,數(shù)據(jù)提取是至關重要的一環(huán),Python 提供了許多強大的工具,其中之一就是 parsel 庫,下面我們就來深入學習一下Parsel的兩種數(shù)據(jù)提取方式吧2023-12-12python解析html開發(fā)庫pyquery使用方法
PyQuery是一個類似于jQuery的Python庫,也可以說是jQuery在Python上的實現(xiàn),能夠以jQuery的語法來操作解析 HTML 文檔,易用性和解析速度都很好2014-02-02Python中的進程操作模塊(multiprocess.process)
這篇文章介紹了Python中的進程操作模塊(multiprocess.process),文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05