如何添加用户输入到一个URL与Tkinter?



我正在尝试创建一个程序,其中货币转换是至关重要的。我有一个API URL,我想添加用户输入(应该被转换的数量),而不是在标签中显示它,而是在以后使用它。

input = Entry(root)
URL = "https://www.myawesomeurl.com/exchangerate/amout="
payload = {}
headers = {"apikey": "awjdwahduwahdwauduw"}
response = requests.request("GET", URL, headers=headers, data = payload)
status_code = response.status_code
result = response.text

我是一个编程新手,所以任何帮助和提示都将非常感激。

可以使用submit函数来处理数据。交货。

import tkinter
import requests
root = tkinter.Tk()

def submitamount():
amounttext = amount.get()
URL = f"https://www.myawesomeurl.com/exchangerate/amout={amounttext}"
payload = {}
headers = {"apikey": "awjdwahduwahdwauduw"}
response = requests.request("GET", URL, headers=headers, data=payload)
status_code = response.status_code
result = response.text
# Do stuff here / call another function to do stuff
#
# If you want to make some variables accessible to other parts of 
# the code, simply define them before this function is declared; 
# Ex.
#
# Instead of:
#   root = tkinter.Tk()
#   ...
#   def submitamount():
#        ...
#
# Do:
#   root = tkinter.Tk()
#   ...
#   response = None
#   status_code = None
#   result = None
#
#   def submitamount():
#        ...
#

amount = tkinter.Entry(root)
amount.pack()
submit = tkinter.Button(text="Submit", command=submitamount)
submit.pack()
root.mainloop()

如果你需要更详细的解释,请告诉我!

最新更新