类型错误:login() 缺少 2 个必需的位置参数:“用户名"和“密码" [英] TypeError: login() missing 2 required positional arguments: 'username' and 'password'
问题描述
我有以下代码:
from tkinter import *从客户进口客户从管理员导入管理员从帐户导入帐户从 tkinter 导入 ttk根 = Tk()全球用户名全局密码定义登录(自我,用户名,密码):名称 = 用户名.get()密码短语 = password.get()msg = self.customer_login(姓名,密码)root.config(高度=500,宽度=500)框架 = 框架(根)标签(根,文本=登录").网格(行= 0,列跨= 4)标签(root, text="用户名").grid(row=3,sticky=W,padx=4)username = Entry(root).grid(row=3, column=2,sticky=W, pady=4)标签(root, text="密码").grid(row=4,sticky=W,padx=4,)密码 = Entry(root, show="*").grid(row=4, column=2,sticky=W, pady=4)loginButton = Button(root, text="登录", width=40)loginButton.grid(row=5, columnspan=4, pady=4, padx=4)loginButton.bind("", 登录)root.mainloop()
我不断收到的错误是:
Tkinter 回调中的异常回溯(最近一次调用最后一次):文件C:\Python34\lib\tkinter\__init__.py",第 1533 行,在 __call__ 中返回 self.func(*args)类型错误:login() 缺少 2 个必需的位置参数:用户名"和密码"
为什么会出现此错误?
您试图仅将一个参数传递给实际上不需要参数但确实需要 3 个参数的方法.>
当回调 login
附加到 bind
方法时,它会发送一个表示事件信息的位置参数,通常命名为 event
即使没有像你那样明确传递.在这种情况下,假定 self
表示事件位置参数.虽然现在 login
缺少两个位置参数,username
和 password
.现在,如果您确实需要这些变量,我会提供不同的答案,但由于您不需要,这是我能想到的最简单的方法.
替换:
def login(self, username, password):
与:
def 登录(事件):
另外,请参阅如何将参数传递给 Tkinter 中的 Button 命令?,因为您可能想要使用 command
一个按钮的选项,而不是一个点击事件,如果用键盘按下按钮,不会调用该方法,还有 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.
这篇关于类型错误:login() 缺少 2 个必需的位置参数:“用户名"和“密码"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!