tkinter错误缺少参数:' self'

时间:2015-12-29 19:06:35

标签: python python-3.x tkinter

学习python并跳转到tkinter。请帮忙。我收到以下错误:

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 个答案:

答案 0 :(得分:0)

要想扩展@Rockybilly的问题 - 您是否正在尝试定义一个类?这就是你所编写的__init__函数的样子,但你实际上并没有把这个类本身。例如,您需要看起来像:

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