采用python不存在的参数
是的,多亏了你,我克服了错误执行顺序的问题,但我有更多的问题 xcv.py(前welcome.py) 定义.py采用python不存在的参数,python,class,arguments,typeerror,Python,Class,Arguments,Typeerror,是的,多亏了你,我克服了错误执行顺序的问题,但我有更多的问题 xcv.py(前welcome.py) 定义.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.
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()
此外,我想我在定义类方面做得很糟糕,但我不知道如何使它们更好
- 编辑 好的,我现在明白了。我忘了我应该在课堂上的定义中加上“自我”。 关闭
self
是一个表示类实例的参数,并且始终是类实例调用的任何函数的第一个参数
假设我们有A级
class A:
def b(self):
print "hello"
要调用函数b,我首先需要创建一个
>>> my_a = A()
>>> my_a.b()
hello
现在你可能想知道。。“如果没有给出self
参数,为什么a.b()可以工作?”
这是因为当您使用类的实例调用函数时,它会自动将该实例作为第一个参数
为了让它更有意义,你可以这样想(这确实有效)
您忘记命名
self
,因为NewLabel()
NewLabel
的参数确实有一个self
参数。这不是第一次。(也许您应该将其重命名为master
或其他名称,因为这更具描述性)与您的问题无关,您使用的StringVar
是错误的。您正在创建一个StringVar
以与一个条目相关联,但随后创建了第二个StringVar
,该条目不被任何人使用。您需要创建一个StringVar
。
class A:
def b(self):
print "hello"
>>> my_a = A()
>>> my_a.b()
hello
>>> my_a = A()
>>> A.b(my_a)
hello