【问题标题】:python - tkinter grid row not appearing and not passing to function correctlypython - tkinter 网格行没有出现并且没有正确传递
【发布时间】:2017-10-03 14:30:46
【问题描述】:

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

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

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

【问题讨论】:

  • 顺便说一句,您不需要使用标签进行间距。您可以为此使用填充。
  • @BryanOakley,我不认为这个问题与您引用的帖子重复。这个问题问题与passVal 方法中缺少的self 参数更相关。是的,还有在 init 上执行命令的问题,但这不是主要问题/问题。
  • @Mike-SMT:很公平。

标签: python python-3.x function tkinter grid


【解决方案1】:

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

变化:

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) 很简单,我们在小部件的顶部和底部放置了空间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多