TypeError:login()缺少2个必需的位置参数:“用户名"和“密码" [英] TypeError: login() missing 2 required positional arguments: 'username' and 'password'

查看:946
本文介绍了TypeError:login()缺少2个必需的位置参数:“用户名"和“密码"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

from tkinter import *
from customer import Customer
from admin import Admin
from account import Account
from tkinter import ttk
root = Tk()

global username
global password


def login(self, username, password):
    name = username.get()
    passphrase = password.get()
    msg = self.customer_login(name, passphrase)




root.config(height=500, width=500)
frame = Frame(root)
Label(root, text="Login").grid(row=0, columnspan=4)

Label(root, text="Username").grid(row=3, sticky=W, padx=4)
username = Entry(root).grid(row=3, column=2, sticky=W, pady=4)
Label(root, text="Password").grid(row=4, sticky=W, padx=4,)
password = Entry(root, show="*").grid(row=4, column=2, sticky=W, pady=4)

loginButton = Button(root, text="Login", width=40)
loginButton.grid(row=5, columnspan=4, pady=4, padx=4)
loginButton.bind("<Button-1>", login)


root.mainloop()

我一直收到的错误是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
TypeError: login() missing 2 required positional arguments: 'username' and 'password'

为什么会出现此错误?

解决方案

您正在尝试仅将一个参数传递给实际上不需要参数但需要需要3个参数的方法.

当将回调login附加到bind方法时,将向其发送一个位置参数,该位置参数表示有关事件的信息,通常命名为event,即使未像您那样显式传递.在这种情况下,假定self表示事件位置参数.尽管现在login缺少两个位置参数,usernamepassword.现在,如果您确实需要这些变量,我会提供一个不同的答案,但是由于您不需要,这是我能想到的最简单的方法.

替换:

def login(self, username, password):

具有:

def login(event):

此外,请参见如何在Tkinter中将参数传递给Button命令?,因为您可能想使用按钮的选项,而不是单击事件,如果用键盘按下按钮,则不会调用该方法,并且bindcommand的作用类似,因为它们都需要一个回调函数.唯一的区别是bind隐式传递第一个位置参数.

I have the code below:

from tkinter import *
from customer import Customer
from admin import Admin
from account import Account
from tkinter import ttk
root = Tk()

global username
global password


def login(self, username, password):
    name = username.get()
    passphrase = password.get()
    msg = self.customer_login(name, passphrase)




root.config(height=500, width=500)
frame = Frame(root)
Label(root, text="Login").grid(row=0, columnspan=4)

Label(root, text="Username").grid(row=3, sticky=W, padx=4)
username = Entry(root).grid(row=3, column=2, sticky=W, pady=4)
Label(root, text="Password").grid(row=4, sticky=W, padx=4,)
password = Entry(root, show="*").grid(row=4, column=2, sticky=W, pady=4)

loginButton = Button(root, text="Login", width=40)
loginButton.grid(row=5, columnspan=4, pady=4, padx=4)
loginButton.bind("<Button-1>", login)


root.mainloop()

The error I keep getting is:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
TypeError: login() missing 2 required positional arguments: 'username' and 'password'

Why do I get this error?

解决方案

You're trying to pass only one argument to a method that in actuality needs no arguments but does require 3 arguments.

When a callback, login, is attached to a bind method, it is sent a positional argument that represents the information about the event, usually named event even though not explicitly passed like you did. In which case it is assumed that self is representing the event positional argument. Though now login lacks two positional arguments, username and password. Now if you actually needed those variables I'd have provided a different answer but since you don't this is the simplest I can think of.

Replace:

def login(self, username, password):

with:

def login(event):

Also, see How to pass arguments to a Button command in Tkinter? as you probably would want to use commandoption of a button as opposed to a click event which won't call the method if the button is pressed with keyboard, and also bind and command act similarly in the sense that they both require a callback function. The only difference is that bind implicitly passes a 1st positional argument.

这篇关于TypeError:login()缺少2个必需的位置参数:“用户名"和“密码"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆