当我尝试将employeeNum
和employeePass
传递给名为displayButton的函数时,它告诉我我错过了这两个参数。
from tkinter import *
def displayButton(root,employNum, employPass):
Label(root,text = employNum ).grid(row = 3, column = 1, sticky = N+S+W+E)
Label(root, text = employPass).grid(row = 4, column = 1, sticky = N+S+W+E)
def main():
root = Tk()
Label(root, text = 'Employee Number: ').grid(row = 0, column = 0, sticky = W)
Label(root, text = 'Login Password: ').grid(row = 1, column = 0, sticky = W)
employeeNum = StringVar()
employeePass = StringVar()
Entry(root, textvariable = employeeNum).grid(row = 0, column = 1, columnspan = 2, sticky = W)
Entry(root, textvariable = employeePass).grid(row = 1, column = 1, columnspan = 2, sticky = W)
checkButton = BooleanVar()
Checkbutton(root, text = 'Remember Me', variable = checkButton).grid(row = 2, column = 1, sticky = W)
Button(root, text = 'Save', relief = RAISED).grid(row = 2, column = 2, sticky = E)
display = Button(root, text = 'Display', relief = RAISED, command = lambda: displayButton(root,employeeNum,employeePass))
display.grid(row = 3, column = 2, sticky = E)
display.bind('<Button-1>', displayButton)
Label(root, text = "Employee's number is ").grid(row = 3, column = 0, sticky = W)
Label(root, text = "Employee's Passowrd is ").grid(row =4 , column = 0, sticky = W)
root.mainloop()
main()
它给我的错误是:
TypeError: displayButton() missing 2 required positional arguments: 'employNum' and 'employPass'
答案 0 :(得分:3)
您无需绑定按钮,因此可以删除此行。
display.bind('<Button-1>', displayButton)
如果您正在使用StringVar()
,并且想要阅读其内容,则必须使用.get()
,employNum.get()
和employPass.get()
。
此代码应该有效:
from Tkinter import *
def displayButton(root,employNum, employPass):
Label(root,text = employNum.get() ).grid(row = 3, column = 1, sticky = N+S+W+E)
Label(root, text = employPass.get()).grid(row = 4, column = 1, sticky = N+S+W+E)
def main():
root = Tk()
Label(root, text = 'Employee Number: ').grid(row = 0, column = 0, sticky = W)
Label(root, text = 'Login Password: ').grid(row = 1, column = 0, sticky = W)
employeeNum = StringVar()
employeePass = StringVar()
Entry(root, textvariable = employeeNum).grid(row = 0, column = 1, columnspan = 2, sticky = W)
Entry(root, textvariable = employeePass).grid(row = 1, column = 1, columnspan = 2, sticky = W)
checkButton = BooleanVar()
Checkbutton(root, text = 'Remember Me', variable = checkButton).grid(row = 2, column = 1, sticky = W)
Button(root, text = 'Save', relief = RAISED).grid(row = 2, column = 2, sticky = E)
display = Button(root, text = 'Display', relief = RAISED, command = lambda: displayButton(root, employeeNum,employeePass))
display.grid(row = 3, column = 2, sticky = E)
#display.bind('<Button-1>', displayButton)
Label(root, text = "Employee's number is ").grid(row = 3, column = 0, sticky = W)
Label(root, text = "Employee's Passowrd is ").grid(row =4 , column = 0, sticky = W)
root.mainloop()
main()