TypeError:get()缺少1个必需的位置参数:'self'(StringVar)

时间:2017-12-11 12:56:58

标签: python tkinter

我正在尝试在tkinter上创建应用程序,当我按do_it按钮时使用work时出现错误。

这是我的代码:

from tkinter import *

root = Tk()
root.title('Wiki Scrape')
root.geometry('640x640+0+0')



heading = Label(root, text = "WELCOME", font =('arial', 30, 'bold')).pack()
label1 = Label(root, text = 'Please type a link from Wikipedia: ', font=
('arial', 10, 'bold'), fg='black').place(x=10, y=200)
name = StringVar
entrybox = Entry(root, textvariable=name, width = 25, 
bg='lightgrey').place(x=250, y=202)


def do_it():
    print(name.get())

work = Button(root, text='Done', width=15, height=1, 
command=do_it).place(x=400, y=198)
root.mainloop()

这是我得到的错误:

Exception in Tkinter callback
Traceback (most recent call last):
 File "C:\Users\et400\AppData\Local\Programs\Python\Python36-
32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:/Users/et400/Downloads/Coding/Python/WebScraping/WikiScrape.py", 
line 18, in do_it
    print(name.get())
TypeError: get() missing 1 required positional argument: 'self'

1 个答案:

答案 0 :(得分:2)

你初始化了除StringVar之外的所有tkinter对象,你应该这样做:

name = StringVar()

而不是name = StringVar

现在你的变量名 - StringVar类的实例和实例自动传递参数'self'。