python tkinter网格行未出现并且未正确传递到函数

2024-09-28 05:28:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码中出现了两个不同的问题

首先,我无法显示网格的第四行,尽管第五行显示得很好。你知道吗

其次,我的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()

Tags: textselfmaintkinterdefcolumnonelabel
1条回答
网友
1楼 · 发布于 2024-09-28 05:28:59

除了缩进问题之外,类中的所有方法都需要传递self作为第一个参数,除非使用特殊标记使其成为独立函数。你知道吗

更改:

def passVal(pw):

收件人:

def passVal(self, pw):

您还需要将Enter按钮上的命令更改为使用lambda,以防止python在启动时调用passVal方法。你知道吗

更改:

command=self.passVal(self.passEntry.get())

收件人:

command=lambda: self.passVal(self.passEntry.get())

这里不需要使用lambda,甚至不需要传递self.passEntry.get获取(). 您可以使用self.passEntry.get()而不是pw来获取passVal()方法中输入字段的值。你知道吗

如果您更改此项:

command=lambda: self.passVal(self.passEntry.get())

对此:

command=self.passVal

还有这个:

def passVal(self, pw):
    if len(pw) < 9:
        print ("no")

对此:

def passVal(self):
    if len(self.passEntry.get()) < 9:
        print ("no")

您的程序运行良好,可以避免在命令中使用lambda。你知道吗

注意:不需要将标签用作间隔符。可以在网格放置中简单地使用padx和pady。你知道吗

请看下面的代码:

import tkinter

class AccountCreation:
    def __init__(self):
        self.main = tkinter.Tk()
        self.main.title("Account Creation")

        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,pady=(10,10))

        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,pady=(10,10))

        tkinter.mainloop()

    def passVal(self, pw):
        if len(pw) < 9:
            print ("no")

    def cancel(self):
        self.main.destroy()

my_gui = AccountCreation()

注意,简单使用pady=(10,10)我们在小部件的顶部和底部放置了空间。你知道吗

相关问题 更多 >

    热门问题