ValueError: 必须指定标题或 pageid

brr*_*rrt -1 python tkinter wikipedia-api

我一直在用 Python 3.8 编写代码来帮助我研究东西,我想把它变成一个 GUI。当我这样做时,每当我点击“开始”按钮时都会收到此错误。如果你们中的任何人看到错误,这里是代码。

# 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()
Run Code Online (Sandbox Code Playgroud)

Ran*_*vis 5

你正在传递Termwikipedia.summary()。错误来自summary()创建page( code ) 时。当没有有效的标题或页面 ID 传递给page( code )时,会发生此错误。在您的情况下会发生这种情况,因为您Term直接传递给summary(),而没有先将其转换为字符串。此外,Term是一个NoneType对象,因为您实际上将它设置为place(). 您必须Term在创建 时存储Entry(),然后对其应用place操作,以便能够保留对它的引用(请参阅此处了解原因):

Term = Entry(root, font = ('Arial, 15'))
Term.place(y = 250, x = 325)
Run Code Online (Sandbox Code Playgroud)

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

Result = wikipedia.summary(Term.get())
Run Code Online (Sandbox Code Playgroud)

  • @TheLizzard 好的,感谢您指出这一点,我更新了我的答案。 (2认同)
  • @brrrrrrrt 没问题,还要记住,如果首先确保将“Term”设置为“Entry()”而不是“place()”的结果,“get()”将无法工作。 (2认同)
  • `Term.get()` 是解决方案,但除非您也将 `Term = Entry(...).place(...)` 分成两行,就像我在示例中所示,否则它将不起作用。有关原因的解释,请参阅@TheLizzard 评论中的链接。 (2认同)