如何显示从字典 API 中提取的文本?

How do I display text being pulled from the dictionary api?

提问人:Alex Howell 提问时间:11/17/2023 最后编辑:toyota SupraAlex Howell 更新时间:11/17/2023 访问量:33

问:

我让它在字典中有一个字典,但无法显示信息。我尝试使用字符串变量,但它对我不起作用。

我试图让它显示我正在使用的字典中的文本或统计数据。我被告知使用文本框进行显示,但我不知道如何将其合并到我的代码中

import tkinter as tk
import http.client as SuperheroDictionary
from ast import literal_eval

# Variables
Powerstats = {}
Biography = {}
Work = {}
Images = {}

#Database connections
conn = SuperheroDictionary.HTTPSConnection("superhero-search.p.rapidapi.com")
headers = {
 'X-RapidAPI-Key': "15cf40731cmsh0b0d641628dcaa8p1231a2jsnc91f57193d96",
 'X-RapidAPI-Host': "superhero-search.p.rapidapi.com"}

# Functions
def search():
 conn.request("GET", f"/api/?hero={SearchBar.get()}", headers=headers)
 res = conn.getresponse()
 data = res.read()
 results = literal_eval(data.decode("utf-8"))

 Powerstats = results['powerstats']
 print(Powerstats)
 Appearance = results['appearance']
 print(Appearance)
 Biography = results['biography']
 print(Biography)
 Work = results['work']
 print(Work)
 Images = results['images']
 print(Images)
 return Powerstats, Appearance, Biography, Work, Images

def display():
 x_var.set(list(results))

# Create Window
window = tk.Tk()
window.geometry("800x600")
window.title("Superheroes")

# Widgets
MainTitle = tk.Label(window, text="Superhero's Dictionary!", font=('Ariel', 40))
MainTitle.place(x=50, y=50)
SearchBar = tk.Entry(window, width=20)
SearchBar.place(x=60, y=125)
SearchButton = tk.Button(window, text="Search", command=search, bd= 4,)
SearchButton.place(x=200, y=125)
button2 = tk.Button(window, text="Display", command=display).place(x= 290, y= 125)
intelligence = tk.Label(window, text="Stats: ")
intelligence.place(x=60, y=200)
C = tk.Canvas(window, height=300, width=400, bd=3)
C.place(x=550, y=600)
x_var = StringVar()
tk.Label(mainframe, textvariable=x_var).place(x=60, y=210)
intelligence1 = tk.Label(window, text=f"Stats: {Powerstats} ")
intelligence1.place(x=60, y=220)
intelligence2 = tk.Label(window, text=f"Stats: {Appearance}")
intelligence2.place(x=60, y=240)
intelligence3 = tk.Label(window, text=f"Stats: {Biography}")
intelligence3.place(x=60, y=260)
intelligence4 = tk.Label(window, text=f"Stats: {Work}")
intelligence4.place(x=60, y=280)
intelligence5 = tk.Label(window, text=f"Stats: {Images}")
intelligence5.place(x=60, y=300)



window.mainloop()
python-3.x 字典 tkinter

评论


答: 暂无答案