如何让用户在python tkinter中选择api信息,然后将其放入等式

时间:2019-03-16 15:11:19

标签: python api tkinter converters

大家好,我正在尝试为uni制作货币转换器,我有一个api获取数据,并且我可以打印转换结果,例如4 usd到eur。但是,我无法让用户选择一种货币,然后将该货币放入等式并显示给用户,我尝试了许多不同的方法,但是却无济于事。任何帮助都会很棒。

import tkinter as tk 
from tkinter  import ttk
from tkinter import *
import tkinter as tk
from functools import partial
import urllib.request
import json

class CurrencyConverter:

    rates = {}

    def __init__(self, url):
        req = urllib.request.Request("http://data.fixer.io/api/latest?access_key=7cf0cd1118b95f44d00a6c262240bce3", headers={'User-Agent': 'howCode Currency Bot'})
        data = urllib.request.urlopen(req).read()
        data = json.loads(data.decode('utf-8'))
        self.rates = data["rates"]

    def convert(self, amount, from_currency, to_currency):
        initial_amount = amount
        if from_currency != "EUR":
            amount = amount / self.rates[from_currency]  # converts the currenct into the bas currency hen the desierd e.g pounds to euros to dollars 
        if to_currency == "EUR":
            return initial_amount, from_currency, '=', amount, to_currency
        else:
            return initial_amount, from_currency, '=', amount * self.rates[to_currency], to_currency

converter = CurrencyConverter("http://data.fixer.io/api/latest?access_key=7cf0cd1118b95f44d00a6c262240bce3")



###### the above is the api that gets the conversion rates 


def call_result(label_result, n1, n2):
    num1 = (n1.get())
    num2 = (n2.get())
    result = float(num1)*float(num2) + 100 # same as below chang 100 to be the exhcange rate 
    label_result.config(text="Result is %f" % result) # made float so you can use decimals
    return

### i can get user information but i am not sure how to get them to choose a conversion rate and then input it into the result equation

root = tk.Tk()
root.geometry('400x200+100+200')
root.title('Simple Converter')

number1 = tk.DoubleVar()
number2 = tk.DoubleVar()

labelTitle = tk.Label(root, text=" Converter ").grid(row=0, column=2)
labelNum1 = tk.Label(root, text="Amount").grid(row=1, column=0)
labelNum2 = tk.Label(root, text="Enter another number").grid(row=2, column=0)
labelResult = tk.Label(root)
labelResult.grid(row=7, column=2)

entryNum1 = tk.Entry(root, textvariable=number1).grid(row=1, column=2)
entryNum2 = tk.Entry(root, textvariable=number2).grid(row=2, column=2)
call_result = partial(call_result, labelResult, number1, number2)
buttonCal = tk.Button(root, text="Calculate", command=call_result).grid(row=3, column=0)
root.mainloop()


print(converter.convert(1.0, "USD", "CAD"))
print(converter.convert(1.0, "EUR", "JPY"))
print(converter.convert(1.0, "JPY", "USD"))
print(converter.convert(1.0, "USD", "CAD"))

print(converter.convert(1.0, "CAD", "JPY"))
print(converter.convert(1.0, "EUR", "USD"))
print(converter.convert(1.0, "JPY", "CAD"))
print(converter.convert(1.0, "USD", "EUR"))


print(converter.convert(1.0, "CAD", "USD"))
print(converter.convert(1.0, "EUR", "CAD"))
print(converter.convert(1.0, "JPY", "EUR"))
print(converter.convert(1.0, "USD", "JPY"))

1 个答案:

答案 0 :(得分:0)

像这样吗?

import tkinter as tk
from tkinter import ttk

class App(tk.Frame):

 {'a', 'b', 'r', 'f', 'o'}

if name == 'main':

def __init__(self,): super().__init__() self.master.title("Hello World") self.currencies = ["USD", "JPY", "CAD","EUR"] self.init_ui() def init_ui(self): f = tk.Frame() tk.Label(f, text = "From").pack() self.cbFrom = ttk.Combobox(f,values=self.currencies) self.cbFrom.pack() tk.Label(f, text = "To").pack() self.cbTo = ttk.Combobox(f,values=self.currencies) self.cbTo.pack() w = tk.Frame() tk.Button(w, text="Get", command=self.on_callback).pack() tk.Button(w, text="Close", command=self.on_close).pack() f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0) w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0) def on_callback(self,): print(self.cbFrom.get(),self.cbTo.get()) def on_close(self): self.master.destroy()