Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-tkinter网格行未显示且未正确传递到函数_Python_Python 3.x_Function_Tkinter_Grid - Fatal编程技术网

python-tkinter网格行未显示且未正确传递到函数

python-tkinter网格行未显示且未正确传递到函数,python,python-3.x,function,tkinter,grid,Python,Python 3.x,Function,Tkinter,Grid,我的代码中出现了两个不同的问题 首先,我无法显示网格的第四行,尽管第五行显示得很好 其次,我的passVal函数不断给出错误: passVal() takes 1 positional argument but 2 were given 我尝试过重新排列东西,并将其转换为字符串,但似乎没有任何效果 我想可能是一件事导致了同样的问题,因为它们都围绕着同一个按钮,但我不确定 import tkinter class AccountCreation: def __init__(self):

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

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

其次,我的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()
导入tkinter
创建类帐户:
定义初始化(自):
self.main=tkinter.Tk()
self.main.title(“账户创建”)
self.topleftLabel=tkinter.Label(text=”“)
self.topleftLabel.grid(行=1,列=1)
self.botrightlab=tkinter.Label(text=”“)
self.botrightLabel.grid(行=5,列=5)
self.promptabel=tkinter.Label(text=“创建至少包含九(9)个字符的密码,\n至少包含一个数字、一个大写字母和一个小写字母。\n\n”)
self.promptabel.grid(行=2,列=2,列span=2)
self.passLabel=tkinter.Label(text=“Password:”)
self.passLabel.grid(行=3,列=2)
self.passEntry=tkinter.Entry(宽度=18,右对齐)
self.passEntry.grid(行=3,列=3)
self.enterButton=tkinter.Button(text=“Enter”\
command=self.passVal(self.passEntry.get())
self.enterButton.grid(行=4,列=2)
self.cancelButton=tkinter.Button(text=“Cancel”\
命令=self.cancel)
self.cancelButton.grid(行=4,列=3)
tkinter.mainloop()
def passVal(pw):
如果len(pw)<9:
打印(“否”)
def取消(自我):
self.main.destroy()
my_gui=AccountCreation()

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

更改:

def passVal(pw):
command=self.passVal(self.passEntry.get())
致:

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

更改:

def passVal(pw):
command=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):
    if len(self.passEntry.get()) < 9:
        print ("no")
这是:

def passVal(self, pw):
    if len(pw) < 9:
        print ("no")
def passVal(self,pw):
如果len(pw)<9:
打印(“否”)
为此:

command=self.passVal
def passVal(self):
    if len(self.passEntry.get()) < 9:
        print ("no")
def passVal(自):
如果len(self.passEntry.get())小于9:
打印(“否”)
您的程序将运行良好,并且可以避免在命令中使用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()
导入tkinter
创建类帐户:
定义初始化(自):
self.main=tkinter.Tk()
self.main.title(“账户创建”)
self.promptabel=tkinter.Label(text=“创建至少包含九(9)个字符的密码,\n至少包含一个数字、一个大写字母和一个小写字母。\n\n”)
self.promptabel.grid(行=2,列=2,列span=2,pady=(10,10))
self.passLabel=tkinter.Label(text=“Password:”)
self.passLabel.grid(行=3,列=2)
self.passEntry=tkinter.Entry(宽度=18,右对齐)
self.passEntry.grid(行=3,列=3)
self.enterButton=tkinter.Button(text=“Enter”\
command=self.passVal(self.passEntry.get())
self.enterButton.grid(行=4,列=2)
self.cancelButton=tkinter.Button(text=“Cancel”\
命令=self.cancel)
self.cancelButton.grid(行=4,列=3,pady=(10,10))
tkinter.mainloop()
def passVal(自身,pw):
如果len(pw)<9:
打印(“否”)
def取消(自我):
self.main.destroy()
my_gui=AccountCreation()

请注意,简单使用
pady=(10,10)
我们在小部件的顶部和底部放置了空格。

顺便说一句,您不需要使用标签作为间距。你可以用填充来回答这个问题。@BryanOakley,我不认为这个问题与你引用的文章重复。这个问题更多地与
passVal
方法中缺少的
self
参数有关。是的,还有在init上执行命令的问题,但这不是主要的问题。@Mike SMT:很公平。