Tkinter Entry Get Error“TypeError:Get()缺少1个必需的位置参数:“self”

2024-10-02 02:27:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图制作一个代码,从Tkinter GUI中获取一个条目,然后通过API进行搜索。 但当我尝试从“条目”小部件获取信息时,显示以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:/Users/User/PycharmProjects/pythonProject/login.py", line 7, in search
    EntryResult = Entry.get()
TypeError: get() missing 1 required positional argument: 'self'

Process finished with exit code 0

这是我的密码:

from tkinter import *
from tkinter.ttk import *
import base64
import requests as r
searchup = Tk()
def search():
    EntryResult = Entry.get()
    hypixelData = r.get('https://api.hypixel.net/player?key={MyAPIKey}&name='+EntryResult).json()
    uuid = hypixelData["player"]["uuid"]
    encodedData = r.get('https://sessionserver.mojang.com/session/minecraft/profile/'+uuid).json()
    encodedCode = encodedData["properties"]["0"]["value"]
    decode = base64.decodestring(encodedCode)
    print(decode)
Label(searchup, text= "Search MineFriends").grid(column = 5, row = 0, sticky = W, pady = 2)
Entry(searchup).grid(column = 5, row = 1, sticky = W, pady = 2)
Button(searchup, text= "Search player!", command=search).grid(column = 5, row = 2, sticky = W, pady = 2)
searchup.mainloop()

我不太清楚这个问题,我是Python新手


Tags: inimportsearchgetuuidtkintercolumngrid
1条回答
网友
1楼 · 发布于 2024-10-02 02:27:29

需要对Entry实例调用get方法。您正在Entry上调用它

该解决方案类似于下面的代码,其中Entry的实例被创建并保存在名为search_box的变量中。然后调用search_boxget方法来获取输入

def search():
    entryResult = search_box.get()
    ...
...
search_box = Entry(searchup)
search_box.grid(column = 5, row = 1, sticky = W, pady = 2)
...

相关问题 更多 >

    热门问题