【问题标题】:python non-existing argument taken采用python不存在的参数
【发布时间】:2014-05-28 21:34:56
【问题描述】:

是的,所以我已经克服了执行顺序错误的问题,多亏了你,但我还有更多。

xcv.py(以前的welcome.py)

#!/usr/bin/env python2.7
#-*- encoding: utf-8 -*
from definitions import *

def NewLabel(text, column, row, self):
    label=Label(self, text=text)
    label.grid(column=column,row=row)

def NewButton(text, action, column, row, self, sticky="N"):
    button=Button(self, text=text, command=action)
    button.grid(column=column,row=row,sticky=sticky)

def OnExit():
    quit()

class encode(Tk):
    def __init__(self,parent):
        Tk.__init__(self,parent)
        self.parent=parent
        self.initialize()

    def initialize(self):
        widgets().NewLabel(u'Welcome to Brunhilda GUI alpha v0.0',0,0,self)
        widgets().NewLabel(u'What do you want to do?',0,1,self)

 #       widgets().NewButton(u'1. Encrypt file',OnEncode(None),0,2,self)
 #       widgets().NewButton(u'2. Decrypt file',OnDecode,0,3,self)
 #       widgets().NewButton(u'Exit',actions().OnExit(),0,4,self)


if __name__=="__main__":
    app=encode(None)
    app.title('Brunhilda GUI v0.0 encoder')
    app.mainloop()

定义.py

from Tkinter import *
import decoding
import encoding
import zxc

version="v0.0"

class widgets():
    def NewLabel(text, column, row, self):
        label=Label(self, text=text)
        label.grid(column=column,row=row)

    def NewEntry(self, text, column, row, action, key='<Return>', sticky="EW"):
        entry=Entry(self, textvariable=StringVar())
        entry.grid(column=column, row=row, sticky=sticky)
        entry.bind(key, action)
        StringVar().set(text)

    def NewButton(text, action, column, row, self, sticky="N"):
        button=Button(self, text=text, command=action)
        button.grid(column=column,row=row,sticky=sticky)
        print asdf
class actions():
    def OnEncode(self):
        try:
            zxc.encode(self)
            quit()
        except KeyboardInterrupt:
            print "goodbye"
            quit()

    def OnDecode():
        try:
            decoding.decode(version)
        except KeyboardInterrupt:
            print "Goodbye"
            quit()

    def OnExit():
        quit()

当调用任何小部件或操作时,它会不断告诉我它被赋予了太多参数

TypeError: NewLabel() takes exactly 4 arguments (5 given)

另外,我想我在定义类方面做得很糟糕,但我不知道如何让它们更好

  • 编辑 好的,我现在明白了。我忘了我应该在课堂上的定义中添加“自我”。 关闭

【问题讨论】:

  • 您忘记将self 命名为NewLabel() 的参数。
  • NewLabel 确实有一个 self 参数。这不是第一个。 (也许您应该将其重命名为 master 或其他名称,因为这样更具描述性)
  • 与您的问题无关,您使用的StringVar 错误。您正在创建一个 StringVar 以与一个条目关联,但随后创建了第二个 StringVar,它没有被使用。你需要创建一个StringVar

标签: python class arguments typeerror


【解决方案1】:

self 是表示类实例的参数,并且始终是类实例调用的任何函数的第一个参数。

假设我们有 A 类

class A:
    def b(self):
        print "hello"

要调用函数b,我首先需要创建一个A的实例

>>> my_a = A()
>>> my_a.b()
hello

现在您可能想知道..“如果没有给定 self 参数,为什么 a.b() 会起作用?”

这是因为当您使用类的实例调用函数时,它会自动将该实例作为第一个参数!

为了更有意义,你可以这样想(这确实有效)..

>>> my_a = A()
>>> A.b(my_a)
hello

【讨论】:

    猜你喜欢
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 2016-02-09
    • 1970-01-01
    • 2017-12-05
    • 2022-01-04
    相关资源
    最近更新 更多