Python为开发GUI(图形用户界面)提供了多种选项。在所有的GUI方法中,tkinter是最常用的方法。它是Python附带的Tk GUI工具包的标准Python接口。Python with tkinter是创建GUI应用程序的最快和最简单的方法。

创建一个tkinter:

1)导入模块tkinter

2)创建主窗口(容器)

3)将任意数量的小部件添加到主窗口。

4)将事件触发器应用于小部件。

让我们创建一个基于GUI的简单实时货币转换器,该转换器可以将金额从一种货币转换为另一种货币。

所需模块:

tkinter

requests

json

下面是实现:

# import all functions from the tkinter

from tkinter import *

# Create a GUI window

root = Tk()

# create a global variables

variable1 = StringVar(root)

variable2 = StringVar(root)

# initialise the variables

variable1.set("currency")

variable2.set("currency")

# Function to perform real time conversion

# from one currency to another currency

def RealTimeCurrencyConversion():

# importing required libraries

import requests, json

# currency code

from_currency = variable1.get()

to_currency = variable2.get()

# enter your api key here

api_key = "Your_Api_Key"

# base_url variable store base url

base_url = r"https://www.alphavantage.co/query?function = CURRENCY_EXCHANGE_RATE"

# main_url variable store complete url

main_url = base_url + "&from_currency =" + from_currency +

"&to_currency =" + to_currency + "&apikey =" + api_key

# get method of requests module

# return response object

req_ob = requests.get(main_url)

# json method return json format

# data into python dictionary data type.

# result contains list of nested dictionaries

result = req_ob.json()

# parsing the required information

Exchange_Rate = float(result["Realtime Currency Exchange Rate"]

['5. Exchange Rate'])

# get method of Entry widget

# returns current text as a

# string from text entry box.

amount = float(Amount1_field.get())

# calculation for the conversion

new_amount = round(amount * Exchange_Rate, 3)

# insert method inserting the

# value in the text entry box.

Amount2_field.insert(0, str(new_amount))

# Function for clearing the Entry field

def clear_all() :

Amount1_field.delete(0, END)

Amount2_field.delete(0, END)

# Driver code

if __name__ == "__main__" :

# Set the background colour of GUI window

root.configure(background = 'light green')

# Set the configuration of GUI window (WidthxHeight)

root.geometry("400x175")

# Create welcome to Real Time Currency Convertor label

headlabel = Label(root, text = 'welcome to Real Time Currency Convertor',

fg = 'black', bg = "red")

# Create a "Amount :" label

label1 = Label(root, text = "Amount :",

fg = 'black', bg = 'dark green')

# Create a "From Currency :" label

label2 = Label(root, text = "From Currency",

fg = 'black', bg = 'dark green')

# Create a "To Currency: " label

label3 = Label(root, text = "To Currency :",

fg = 'black', bg = 'dark green')

# Create a "Converted Amount :" label

label4 = Label(root, text = "Converted Amount :",

fg = 'black', bg = 'dark green')

# grid method is used for placing

# the widgets at respective positions

# in table like structure .

headlabel.grid(row = 0, column = 1)

label1.grid(row = 1, column = 0)

label2.grid(row = 2, column = 0)

label3.grid(row = 3, column = 0)

label4.grid(row = 5, column = 0)

# Create a text entry box

# for filling or typing the information.

Amount1_field = Entry(root)

Amount2_field = Entry(root)

# ipadx keyword argument set width of entry space.

Amount1_field.grid(row = 1, column = 1, ipadx ="25")

Amount2_field.grid(row = 5, column = 1, ipadx ="25")

# list of currency codes

CurrenyCode_list = ["INR", "USD", "CAD", "CNY", "DKK", "EUR"]

# create a drop down menu using OptionMenu function

# which takes window name, variable and choices as

# an argument. use * befor the name of the list,

# to unpack the values

FromCurrency_option = OptionMenu(root, variable1, *CurrenyCode_list)

ToCurrency_option = OptionMenu(root, variable2, *CurrenyCode_list)

FromCurrency_option.grid(row = 2, column = 1, ipadx = 10)

ToCurrency_option.grid(row = 3, column = 1, ipadx = 10)

# Create a Convert Button and attached

# with RealTimeCurrencyExchangeRate function

button1 = Button(root, text = "Convert", bg = "red", fg = "black",

command = RealTimeCurrencyConversion)

button1.grid(row = 4, column = 1)

# Create a Clear Button and attached

# with delete function

button2 = Button(root, text = "Clear", bg = "red",

fg = "black", command = clear_all)

button2.grid(row = 6, column = 1)

# Start the GUI

root.mainloop()

输出:

python编写程序实现货币转换_使用Tkinter的Python实时货币转换器相关推荐

  1. python运行程序为什么会卡住_为什么我的 Python 程序卡住啦!

    本文简答介绍在linux环境下如何利用gdb来分析卡住的程序,本文使用的Python为Cpython2.7,操作系统为Debian. 阻塞在IO 程序被卡住,很可能是程序被阻塞了,即在等待(wait) ...

  2. python语言程序设计基础上海交通大学_北京交通大学:Python语言程序设计

    『课程目录』:$ y0 q8 G3 Q" Z% p6 K│ ├─第一章概述, c) e/ m) X# s- B, r│ │ 1.1.1第1课时计算机起源 – 计算机发展史中三位里程碑人物,快 ...

  3. python编写程序掷骰子游戏规则_Python Tkinter实例――模拟掷骰子

    什么是Tkinter? Tkinter 是 Python 的标准 GUI 库.Python 使用 Tkinter 可以快速的创建 GUI 应用程序. 由于 Tkinter 是内置到 python 的安 ...

  4. python安装程序打不开_使用PIP安装Python包会导致链接:致命错误LNK1104:无法打开文件“python27.lib”...

    我试图让PIP安装Twisted,但我得到了这个链接错误. 我使用的是64位Windows8和Python2.7.10. 我认为这个问题与visualstudio编译器有关.我有2009年和2010年 ...

  5. python编写一个投票选班长_一个简单的Python自动投票

    同学在网上帮朋友投票,就简单写了一个,基本上3秒钟一票,这家投票最简单,没有验证码,没有注册用户限制,没有IP限制,三无~!! 本程序需要pam30模块 #---------------------- ...

  6. 编写python程序、计算账户余额_小明有20w存款存在余额宝中,按余额宝年收益为3.35%计算,用Python编写程序计算,多少年后小明的存款达到30w?...

    [判断题]卤素灯泡是在灯泡内充入氟.氯等卤素气体. [单选题]我国刑法第12条关于溯及力的规定采取的是( ). [填空题]本地局域网 LAN 内, () 和无绳电话速率较低,主流带宽是 100kbps ...

  7. Python入门程序练习题-温度转换

    Python入门程序练习题-温度转换 题目说明: 温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fabrenheit).‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪ ...

  8. [python]编写程序产生 ISBN 号的校验位。

    @MADE BY YWL_XJTU python编写程序产生 ISBN 号的校验位. 编写程序产生 ISBN 号的校验位.任何新出版的图书都配有 ISBN 号, 2007 年以前是由 10 位数字加上 ...

  9. python编写程序解方程_第2章 Python初步 课后题

    [单选题]建设中国特色社会主义,把我国建设成为富强.民主.文明.和谐.美丽的社会主义现代化强国,是我国各族人民的( ) [简答题]案例系统的测试报告,提交时间为9月28日,上午九点之前 [单选题]一份 ...

  10. python编写程序输出诗句_闲来无事能干嘛 用Python来玩诗歌接龙

    闲来无事能干嘛 用Python来玩诗歌接龙 作为一个懂Python爬虫的运维狗,闲来无事的时候总要找点乐子(睡觉不香么),哈哈,就是这么的敬业(其实是无聊).今天网盾科技给大家讲讲怎么用Python爬 ...

最新文章

  1. 有没有什么高效「炼丹」神器可以推荐?复旦fastNLP团队祭出内部调参利器fitlog...
  2. 彻底炸锅了!华为新天才少年出炉:武大94年博士生入选!任正非最新发声:江山代有才人出,不拘一格降人才!...
  3. Myeclipse加载php插件
  4. android AIDL服务
  5. 多么痛的领悟--写在领英股票被腰斩之后
  6. 10个职场故事,让人不得不看
  7. Jsoup(三)-- Jsoup使用选择器语法查找DOM元素
  8. 人月聊IT:对业务系统的可扩展性设计思考
  9. matlab 矩阵引用,MATLAB矩阵生成、引用
  10. adprw指令通讯案例_实例 | 三菱FX3U485无协议通讯程序详解(含程序)
  11. 计算机组成原理——基础知识
  12. shell 参数的分组读取
  13. 曾国藩-200句名言归纳
  14. python爬虫|post的响应,利用python实现有道翻译在线翻译
  15. 升级版微生物16s测序报告|解读
  16. 零基础如何学 Web 前端开发
  17. 21点游戏A计算方法
  18. 实现你人生中的第一个jQuery插件
  19. html5源码笔记【爱创课堂专业前端培训】
  20. springboot整合bboss操作elasticsearch

热门文章

  1. 【HTML5 基础】HTML5重要内容
  2. C++中如何创建一个类?
  3. 大富豪5.3全网首发,真正的5.3正版破解授权,不是高防端
  4. Glide加载图片缩放模式
  5. 大数据分析平台洱源县_洱源县专项债可行性研究报告
  6. matlab的syms无法在函数中使用_matlab syms什么意思_常见问题解析
  7. 【翻译】LearnYouSomeErlangForGreatGood(一):导言
  8. epoll精粹二 - Linux C++网络编程(二十三)
  9. 网络实用技术--登录--第三方登录--百度第三方登录
  10. matlab图上面加箭头,利用matlab如何在图形中绘制箭头