TypeError:login()缺少2个必需的位置参数:“用户名"和“密码" [英] TypeError: login() missing 2 required positional arguments: 'username' and 'password'
问题描述
我有以下代码:
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
缺少两个位置参数,username
和password
.现在,如果您确实需要这些变量,我会提供一个不同的答案,但是由于您不需要,这是我能想到的最简单的方法.
替换:
def login(self, username, password):
具有:
def login(event):
此外,请参见如何在Tkinter中将参数传递给Button命令?,因为您可能想使用bind
和command
的作用类似,因为它们都需要一个回调函数.唯一的区别是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 command
option 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屋!