python 实现一个图形界面的汇率计算器

 更新时间:2020年11月09日 11:40:07   投稿:yxs  
这篇文章主要介绍了python 实现一个图形界面的汇率计算器,帮助大家更好的理解和学习如何制作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 实现一个图形界面的汇率计算器的详细内容,更多关于python 汇率计算的资料请关注脚本之家其它相关文章!

相关文章

  • 教你怎么用python实现字符串转日期

    教你怎么用python实现字符串转日期

    今天教各位小伙伴怎么用python实现字符串转日期,文中有非常详细的代码示例,对正在学习python的小伙伴很有帮助,需要的朋友可以参考下
    2021-05-05
  • Flask使用SQLAlchemy实现持久化数据

    Flask使用SQLAlchemy实现持久化数据

    本文主要介绍了Flask使用SQLAlchemy实现持久化数据,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2021-07-07
  • Python tkinter中四个常用按钮的用法总结

    Python tkinter中四个常用按钮的用法总结

    tkinter中有四个控件被冠以Button之名,分别是:Button, Checkbutton, Radiobutton, Menubutton,下面小编就来和大家聊聊它们的具体用法,感兴趣的可以学习一下
    2023-09-09
  • python主线程捕获子线程的方法

    python主线程捕获子线程的方法

    这篇文章主要为大家详细介绍了python主线程捕获子线程的方法,具有一定的参考价值,感兴趣的朋友可以参考一下
    2018-06-06
  • 简单解析Django框架中的表单验证

    简单解析Django框架中的表单验证

    这篇文章主要介绍了简单解析Django框架中的表单验证,Django是Python重多人气框架中最为著名的一个,需要的朋友可以参考下
    2015-07-07
  • 用Python提取PDF表格的方法

    用Python提取PDF表格的方法

    这篇文章主要介绍了用Python提取PDF表格的方法,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-04-04
  • Python中yield关键字的理解与使用

    Python中yield关键字的理解与使用

    yield关键字用于创建生成器函数,一种高效利用内存的函数类型,可以像迭代器对象一样使用,本文主要介绍了Python中的yield关键字的应用,需要的可以参考下
    2023-08-08
  • Python实现通讯录功能

    Python实现通讯录功能

    这篇文章主要为大家详细介绍了Python实现通讯录功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • Numpy的简单用法小结

    Numpy的简单用法小结

    这篇文章主要介绍了Numpy的简单用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Python爬虫基础之爬虫的分类知识总结

    Python爬虫基础之爬虫的分类知识总结

    来给大家讲python爬虫的基础啦,首先我们从爬虫的分类开始讲起,下文有非常详细的知识总结,对正在学习python的小伙伴们很有帮助,需要的朋友可以参考下
    2021-05-05

最新评论