我的代码中出现了两个不同的问题
首先,我无法显示网格的第四行,尽管第五行显示得很好。你知道吗
其次,我的passVal函数不断给出错误:
passVal() takes 1 positional argument but 2 were given
我试过重新排列东西,并把它转换成一个字符串,但似乎没有任何效果。你知道吗
我想有可能是一件事导致了相同的问题,因为他们都围绕着同一个按钮,但我不确定。你知道吗
import tkinter
class AccountCreation:
def __init__(self):
self.main = tkinter.Tk()
self.main.title("Account Creation")
self.topleftLabel = tkinter.Label(text=" ")
self.topleftLabel.grid(row=1,column=1)
self.botrightLabel = tkinter.Label(text=" ")
self.botrightLabel.grid(row=5,column=5)
self.promptLabel = tkinter.Label(text="Create a password with at least nine (9)\n characters that contains at least one digit, \n one uppercase, and one lowercase letter.\n\n")
self.promptLabel.grid(row=2,column=2,columnspan=2)
self.passLabel = tkinter.Label(text="Password:")
self.passLabel.grid(row=3,column=2)
self.passEntry = tkinter.Entry(width = 18, justify='right')
self.passEntry.grid(row=3,column=3)
self.enterButton = tkinter.Button(text="Enter", \
command=self.passVal(self.passEntry.get()))
self.enterButton.grid(row=4,column=2)
self.cancelButton = tkinter.Button(text="Cancel", \
command=self.cancel)
self.cancelButton.grid(row=4,column=3)
tkinter.mainloop()
def passVal(pw):
if len(pw) < 9:
print ("no")
def cancel(self):
self.main.destroy()
my_gui = AccountCreation()
除了缩进问题之外,类中的所有方法都需要传递self作为第一个参数,除非使用特殊标记使其成为独立函数。你知道吗
更改:
收件人:
您还需要将Enter按钮上的命令更改为使用
lambda
,以防止python在启动时调用passVal
方法。你知道吗更改:
收件人:
这里不需要使用lambda,甚至不需要传递self.passEntry.get获取(). 您可以使用
self.passEntry.get()
而不是pw
来获取passVal()
方法中输入字段的值。你知道吗如果您更改此项:
对此:
还有这个:
对此:
您的程序运行良好,可以避免在命令中使用lambda。你知道吗
注意:不需要将标签用作间隔符。可以在网格放置中简单地使用padx和pady。你知道吗
请看下面的代码:
注意,简单使用
pady=(10,10)
我们在小部件的顶部和底部放置了空间。你知道吗相关问题 更多 >
编程相关推荐