gpt4 book ai didi

python - 类型错误 : leftClick() missing 1 required positional argument: 'password'

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:26 24 4
gpt4 key购买 nike

我是 Python 新手,正在尝试创建一个登录窗口。当用户输入用户名和密码时,程序将检索保存到文本文件中的设置用户名和密码,如果匹配,则用户将能够使用该程序。但是,当我尝试运行该程序时,我不断收到此消息。这是我的代码:

    from tkinter import *
login = Tk()
Label(login, text ="Username").grid(row = 0)
Label(login, text ="Password").grid(row = 1)

username = StringVar()
password = StringVar()

i1 = Entry(login, textvariable=username)
i2 = Entry(login, textvariable=password, show = "*")

i1.grid(row = 0, column = 1)
i2.grid(row = 1, column = 1)

def leftClick(username, password):
f = open('godhelpme.txt', 'r')
file = open('some_file.txt', 'r')
if username == (f.read()):
if password == (file.read()):
print ("HELLOOOO")
else:
print ("invalid username/password")
else: print ("invalid username/password")


button2 = Button(login, text = "Log In", command = save)
button2.grid(columnspan = 2)
button2.bind("<Button-1>", leftClick)



login.mainloop()

这就是我尝试为按钮添加两个功能的方法

    from tkinter import *
from tkinter import ttk


def win1():
mainframe = ttk.Frame(root, padding = '3 3 12 12')
mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)

ttk.Button(mainframe, text = 'Register', command = register).grid(
column = 1, row = 1, sticky = W)
ttk.Button(mainframe, text = 'Log In', command = login).grid(
column = 1, row = 2, sticky = W)

root.mainloop()

def combine_funcs(save, quit):
def combined_func(save, quit):
for f in funcs:
f(save, quit)
return combined_funcs

def quit():
root.withdraw()

def register():
quit()
def save():
import json as serializer
with open('godhelpme.txt', 'w') as f:
serializer.dump(username.get(), f)
with open('some_file.txt', 'w') as f:
serializer.dump(password.get(), f)
register.quit()

register = Tk()
Label(register, text ="Username").grid(row = 0)
Label(register, text ="Password").grid(row = 1)

username = StringVar()
password = StringVar()

e1 = Entry (register, textvariable=username)
e2 = Entry (register, textvariable=password, show= "*")

e1.grid(row = 0, column = 1)
e2.grid(row = 1, column = 1)


button1 = Button(register, text = "Register", command = combined_funcs(save, quit))
button1.grid(columnspan = 2)
button1.bind("<Button-1>")

register.mainloop

最佳答案

绑定(bind)按钮时,回调函数应该是获取事件作为参数的函数。 tkinter 不知道您期望获得什么用户名和密码,因此您无法绑定(bind)此类函数。

您需要创建一个回调函数来获取用户输入的用户名和密码,然后使用这些参数调用 leftClick 函数。

也没有必要使用绑定(bind)到 ,因为您不需要知道单击的坐标。您的代码中已包含 command = save,因此按下按钮时应调用函数 save

您可能想要这样的东西,因为您已经将 Entry 小部件绑定(bind)到变量,因此它们会自动填充:

def clickLogin():
f = open('godhelpme.txt', 'r')
file = open('some_file.txt', 'r')
if username == (f.read()):
if password == (file.read()):
print ("HELLOOOO")
else:
print ("invalid username/password")
else: print ("invalid username/password")


button2 = Button(login, text = "Log In", command = clickLogin)
button2.grid(columnspan = 2)

并且没有调用在按钮上绑定(bind)任何其他内容。

关于python - 类型错误 : leftClick() missing 1 required positional argument: 'password' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30148163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com