ValueError: Either a title or a pageid must be specified

ValueError: Either a title or a pageid must be specified

下面显示的代码给出了一个 ValueError,说明我需要指定标题或页码。我一遍又一遍地检查代码,没有发现问题。如果您知道我做错了什么,请告诉我。

此代码旨在向我提供有关我输入的大多数关键字的信息。如果我要 Jeff Bezos,信息将打印到控制台。

# Imports
import wikipedia
from tkinter import *
import time

# Code
def Application():
    # Definitions
    def Research():
        # Defines Entry
        Result = wikipedia.summary(Term)

        print(Result)

    # Window Specifications
    root = Tk()
    root.geometry('900x700')
    root.title('Wikipedia Research')

    # Window Contents
    Title = Label(root, text = 'Wikipedia Research Tool', font = ('Arial', 25)).place(y = 10, x = 250)

    Directions = Label(root, text = 'Enter a Term Below', font = ('Arial, 15')).place(y = 210, x = 345)

    Term = Entry(root, font = ('Arial, 15')).place(y = 250, x = 325)

    Run = Button(root, font = ('Arial, 15'), text = 'Go', command = Research).place(y = 300, x = 415)

    # Mainloop
    root.mainloop()

# Run Application
Application()

您正在将 Term 传递给 wikipedia.summary()。错误来自 summary() 创建 pagecode). This error happens when there is no valid title or page ID being passed to the page (code). This is happening in your case because you're passing Term straight to summary(), without first converting it to a string. Additionally, Term is a NoneType object, because you're actually setting it to the result of place(). You have to store Term when you create the Entry(), and then apply the place operation to it, in order to be able to keep a reference to it (see here 为什么):

Term = Entry(root, font = ('Arial, 15'))
Term.place(y = 250, x = 325)

然后,您可以通过以下方式获取文本值:

Result = wikipedia.summary(Term.get())