簡體   English   中英

如何使用 api 更新 tkinter 中的標簽?

[英]how to update labels in tkinter using api?

當我搜索一本書時,我正在嘗試更新標簽,但它們沒有更新。 我已經嘗試了幾種不同的方法,但都沒有奏效

代碼:

# find books
def find_books():
    global book
    book = search_book.get()


# UI
text_describe = Label(root, text="Find a book", font=("Helvtica", 30), bg="#dbdbdb")
text_describe.pack(pady=10)

# search box
search_book = Entry(root, width=20, font=("Helvtica", 20), fg="#101010")
search_book.pack(pady=10)

# get books
response = requests.get("https://www.googleapis.com/books/v1/volumes?q=" + book)
data = response.json()

# find books
find = Button(root, text="search", width=20, command=find_books)
find.pack(pady=10)


try:
    for item in data['items']:
        title = item['volumeInfo']['title']
        link = item['volumeInfo']['infoLink']
        thumb = item['volumeInfo']['imageLinks']['thumbnail']
        break

    # Book cover
    book_cover = urllib.request.urlopen(thumb)
    cover_image = PIL.Image.open(book_cover)
    image = PIL.ImageTk.PhotoImage(cover_image)
    cover_label = Label(root, image=image)
    cover_label.image = image  # better to keep a reference of the image
    cover_label.pack(pady=15)

    # Book Title
    title_label = Label(root, text="" + title, bg="#dbdbdb")
    title_label.pack(pady=10)

except KeyError:
    print('key error')

root.mainloop()

我已經嘗試這樣做了一段時間,如果你能幫助我,我無法讓它正常工作。 提前致謝:)

您應該將從互聯網獲取數據的代碼放入find_books() function:

import tkinter as tk
import requests
from requests.exceptions import RequestException
from urllib.request import urlopen
from PIL import Image, ImageTk
#from io import BytesIO  # for using requests to fetch image

# find books
def find_books():
    book = search_book.get().strip()
    if book:
        try:
            # get books
            response = requests.get("https://www.googleapis.com/books/v1/volumes?q=" + book)
            data = response.json()
            if data['totalItems'] > 0:
                for item in data['items']:
                    title = item['volumeInfo']['title']
                    link = item['volumeInfo']['infoLink']
                    thumb = item['volumeInfo']['imageLinks']['thumbnail']

                    # Book cover using urllib
                    book_cover = urlopen(thumb)
                    cover_image = Image.open(book_cover)
                    # or use requests
                    #response = requests.get(thumb)
                    #cover_image = Image.open(BytesIO(response.content))
                    image = ImageTk.PhotoImage(cover_image)
                    cover_label = tk.Label(root, image=image)
                    cover_label.image = image  # better to keep a reference of the image
                    cover_label.pack(pady=15)

                    # Book Title
                    title_label = tk.Label(root, text="" + title, bg="#dbdbdb")
                    title_label.pack(pady=10)

                    # only process one book as your original code
                    break
            else:
                tk.Label(root, text='No book found', fg='red').pack(pady=10)
        except KeyError:
            print('key error')
        except RequestException as e:
            print(e)

root = tk.Tk()

# UI
text_describe = tk.Label(root, text="Find a book", font=("Helvtica", 30), bg="#dbdbdb")
text_describe.pack(pady=10)

# search box
search_book = tk.Entry(root, width=20, font=("Helvtica", 20), fg="#101010")
search_book.pack(pady=10)

# find books
find = tk.Button(root, text="search", width=20, command=find_books)
find.pack(pady=10)

root.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM