【问题标题】:Tk button throws error: Takes exactly 1 argument (0 given) on callTk 按钮抛出错误:在调用时恰好采用 1 个参数(给定 0)
【发布时间】:2017-04-24 16:23:37
【问题描述】:

我的代码中的问题是,当我单击 topLevel() 窗口上的提交按钮时,它给了我标题中列出的错误。如果我尝试输入top.submit_button,则会引发submit_button 不是topLevel() 的一部分的错误。我已经搜索了整个stackoverflow,但没有发现与此类似的问题。我是 Python GUI 编程的新手,非常感谢您的帮助。

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.KTitle = tk.Label(self, text="Login ")
        self.KTitle.grid(row=7,column=0, columnspan=2)
        self.KUsername = tk.Label(self, text="Username: ")
        self.KUsername.grid(row=8,column=0, sticky=E)
        self.KPassword = tk.Label(self, text="Password: ")
        self.KPassword.grid(row=9,column=0, sticky=E)
        self.KUEntry = tk.Entry(self, width=15)
        self.KUEntry.grid(row=8,column=1, sticky=E)
        self.KUPass = tk.Entry(self, width=15)
        self.KUPass.grid(row=9,column=1, sticky=E)

        self.KUSubmit = tk.Button(self, text="Submit")
        self.KUSubmit.grid(row=10, column=0, columnspan=2)

        self.KTitle = tk.Label(self, text=" or ")
        self.KTitle.grid(row=11,column=0, columnspan=2)

        self.KUSubmit = tk.Button(self, text="Create an Account", command=self.create_button)
        self.KUSubmit.grid(row=12, column=0, columnspan=2)

    def create_button(self):

        top = Toplevel()

        top.TitleHead = tk.Label(top, text="Create an Account ")
        top.TitleHead.grid(row=0,column=2, columnspan=3)

        #userInput
        top.f_name = tk.Label(top, text="First Name: ")
        top.f_name.grid(row=1,column=2, sticky=W)
        top.Entry1 = tk.Entry(top, width=15)
        top.Entry1.grid(row=1,column=3, sticky=W)
        top.l_name = tk.Label(top, text="Last Name: ")
        top.l_name.grid(row=2,column=2, sticky=W)
        top.Entry2 = tk.Entry(top,width=15)
        top.Entry2.grid(row=2,column=3, sticky=W)
        top.username = tk.Label(top, text="Username: ")
        top.username.grid(row=3,column=2, sticky=W)
        top.Entry3 = tk.Entry(top, width=15)
        top.Entry3.grid(row=3,column=3, sticky=W)
        top.Ptoken = tk.Label(top, text="Password: ")
        top.Ptoken.grid(row=4,column=2, sticky=W)
        top.Entry4 = tk.Entry(top, width=15)
        top.Entry4.grid(row=4,column=3, sticky=W)

        top.Submit = tk.Button(top, text="Submit", command=submit_button)
        top.Submit.grid(row=5, column=2, columnspan=2)

    def submit_button(top):

        x = top.Entry4.get()
        salt = uuid.uuid4().hex
        hashed_password = hashlib.sha512(x + salt).hexdigest()

        add = ("INSERT INTO User "
               "(ID, username, f_name, l_name, salt, PashHash) "
               "VALUES (%s, %s, %s, %s, %s, %s)")

        ID = int(0)
        username = top.Entry3.get()
        f_name = top.Entry1.get()
        l_name = top.Entry2.get()
        salt = salt
        PashHash = hashed_password

        data = (ID , top.username , top.f_name , top.l_name, salt, PashHash)

        cursor.execute(add, data)
        cnx.commit()

app = SampleApp()
app.mainloop()

【问题讨论】:

  • def submit_button(top) 应该是 def submit_button(self, top) 如果它是类的一部分
  • 请显示完整的错误。
  • 哪个Button?为什么给self.KUSubmit分配两个不同的?
  • @depperm:top 参数的来源是什么(无论您建议的更改是否被放入)?
  • create_button 是在启动时创建的第一个(主)窗口上的按钮。 submit_button 是 topLevel() 窗口底部的按钮。当我尝试按下作为 submit_button 函数的提交按钮时,编译器给我的错误是“create_button 恰好需要 1 个参数(给定 0)”。这是错误,复制并粘贴。并且双 KUSubmit 是复制粘贴错误。

标签: python function button tkinter


【解决方案1】:

问题在于您如何为(第二个)KUSubmit 按钮设置回调函数。首先,由于它是SampleApp 类的方法,它会自动接收self 参数。

其次是传递一个参数给它。虽然可以这样做,但在这种情况下,将值保存在 self.top 属性中并通过其自动 self 参数在方法中从那里检索它会更容易。

我已按照以下内容对您的代码进行了更改,如下所示。有关详细信息,请参阅带有# CHANGED cmets 的行。

import Tkinter as tk
from Tkinter import *

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.KTitle = tk.Label(self, text="Login ")
        self.KTitle.grid(row=7,column=0, columnspan=2)
        self.KUsername = tk.Label(self, text="Username: ")
        self.KUsername.grid(row=8,column=0, sticky=E)
        self.KPassword = tk.Label(self, text="Password: ")
        self.KPassword.grid(row=9,column=0, sticky=E)
        self.KUEntry = tk.Entry(self, width=15)
        self.KUEntry.grid(row=8,column=1, sticky=E)
        self.KUPass = tk.Entry(self, width=15)
        self.KUPass.grid(row=9,column=1, sticky=E)

        self.KUSubmit = tk.Button(self, text="Submit")
        self.KUSubmit.grid(row=10, column=0, columnspan=2)

        self.KTitle = tk.Label(self, text=" or ")
        self.KTitle.grid(row=11,column=0, columnspan=2)

        self.KUSubmit = tk.Button(self, text="Create an Account",
                                  command=self.create_button)
        self.KUSubmit.grid(row=12, column=0, columnspan=2)

    def create_button(self):

        self.top = top = Toplevel()  # CHANGED

        top.TitleHead = tk.Label(top, text="Create an Account ")
        top.TitleHead.grid(row=0,column=2, columnspan=3)

        #userInput
        top.f_name = tk.Label(top, text="First Name: ")
        top.f_name.grid(row=1,column=2, sticky=W)
        top.Entry1 = tk.Entry(top, width=15)
        top.Entry1.grid(row=1,column=3, sticky=W)
        top.l_name = tk.Label(top, text="Last Name: ")
        top.l_name.grid(row=2,column=2, sticky=W)
        top.Entry2 = tk.Entry(top,width=15)
        top.Entry2.grid(row=2,column=3, sticky=W)
        top.username = tk.Label(top, text="Username: ")
        top.username.grid(row=3,column=2, sticky=W)
        top.Entry3 = tk.Entry(top, width=15)
        top.Entry3.grid(row=3,column=3, sticky=W)
        top.Ptoken = tk.Label(top, text="Password: ")
        top.Ptoken.grid(row=4,column=2, sticky=W)
        top.Entry4 = tk.Entry(top, width=15)
        top.Entry4.grid(row=4,column=3, sticky=W)

        top.Submit = tk.Button(top, text="Submit", command=self.submit_button)
        top.Submit.grid(row=5, column=2, columnspan=2)

    def submit_button(self):  # CHANGED

        x = self.top.Entry4.get()  # CHANGED
        salt = uuid.uuid4().hex
        hashed_password = hashlib.sha512(x + salt).hexdigest()

        add = ("INSERT INTO User "
               "(ID, username, f_name, l_name, salt, PashHash) "
               "VALUES (%s, %s, %s, %s, %s, %s)")

        ID = int(0)
        username = top.Entry3.get()
        f_name = top.Entry1.get()
        l_name = top.Entry2.get()
        salt = salt
        PashHash = hashed_password

        data = (ID , top.username , top.f_name , top.l_name, salt, PashHash)

        cursor.execute(add, data)
        cnx.commit()

app = SampleApp()
app.mainloop()

【讨论】:

  • 复制和粘贴您的代码给了我这个错误:Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2 .7/lib-tk/Tkinter.py",第 1536 行,在 call 中 return self.func(*args) 文件 "./Uninote.py",第 123 行,在 create_button top.Submit = tk.Button(top, text="Submit", command=self.submit_button) 文件 "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",第 1898 行,在 getattr 中返回 getattr(self.tk, attr) AttributeError: submit_button
  • 我运行它时没有,但代码可能存在其他问题,因为我没有尝试所有可能的交互。发生错误时您在哪里做什么?
  • 从 Traceback 看来,submit_button 没有被识别为方法,因此执行停止。在我所做的小测试中肯定会发生很多事情,所以我不知道如果您在我的答案中复制代码而不做任何更改会出现什么问题。
  • 你的方法确实有效(顺便谢谢你)。问题在于我的缩进。所以代码编译并运行,但是当我点击 topLevel() 窗口中的提交按钮时,它给了我这个:(下一条评论)
  • Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py ",第 1536 行,在 call 中返回 self.func(*args) 文件 "./Uninote.py",第 128 行,在 submit_button x = self.top.Entry4.get() 文件中"/ System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",第 1898 行,在 getattr 中返回 getattr(self.tk, attr)属性错误:顶部
猜你喜欢
  • 2015-06-06
  • 2023-03-28
  • 1970-01-01
  • 2017-02-19
  • 2013-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-06
相关资源
最近更新 更多