Tk按钮抛出错误:在通话时正好采用1个参数(0给定)

时间:2017-04-24 16:23:37

标签: python function button tkinter

我的代码中的问题是,当我单击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()

1 个答案:

答案 0 :(得分:1)

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

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

我在下面的代码中对这些行进行了更改。请参阅具有# CHANGED条评论的行。

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()