2015-12-29 81 views
-1

刚刚学习python并跳到tkinter。请帮忙。我得到以下错误:tkinter错误缺少参数:'self'

File "E:\Program Files\Python34\lib\tkinter\__init__.py", line 1533, in __call__ 
    return self.func(*args) 
TypeError: open() missing 1 required positional argument: 'self' 

以下是我的Python代码:

from tkinter.filedialog import askopenfilename 

root = tkinter.Tk(className="Its a Full Mad Creation") 
textPad = scrolledtext.ScrolledText(root, width=100, height=80) # creates text area 
def __init__(self): 

    self.file_opt = options = {} 
    options['defaultextension'] = '.php' 
    options['filetypes'] = [('PHP files', '.php'), ('Javascript files', '.js'), ('HTML files', '.htm'), ('HTML files', '.html'), ('CSS files', '.css')] 

def open(self): 
    file = tkinter.filedialog.askopenfile(parent=root, mode='rb', title='Select a php file', **self.file_opt) 
    if file != None: 
     contents = file.read() 
     textPad.insert('1.0', contents) 
     file.close() 

完整的代码是在这里:http://pastie.org/private/swpihqat8eo063z2eo6fng

+1

这个班在哪里? – Rockybilly

+0

http://pastie.org/private/swpihqat8eo063z2eo6fng –

+0

其中是班级名称? –

回答

0

要在@Rockybilly的问题可能扩大 - 你试图定义一个类?这就是你编写的__init__函数的样子,但实际上并没有把类本身放在这里。例如,你需要看起来像这样的东西:

class SomeClass: # This line is missing! 
    def __init__(self, x): 
     ... code ... 
+0

哦对不起,我只是在学习..我忘了..我以为只有def才会工作 –

+0

不用担心。把所有相关的功能放在这样的顶线下,看看是否有效。 – Sophologist

+0

这是否解决了问题? – Sophologist