0

I have been working on this simple text editor using Python 3 and Tkinter. This is my first time creating a program with an actual GUI and I have relied only on Tkinter documentation. I managed to create a text editor. However, we I started working on the commands of the Open menu item. I came across an error. It would really help if anyone could explain to me what I am doing wrong, and the proper way to do it.

from tkinter import *
from tkinter.filedialog import LoadFileDialog
from tkinter.filedialog import SaveFileDialog
from tkinter.filedialog import Directory

class textEditor:

    #main window
    main=Tk()
    main.title("Text Editor")
    textbox=Text(main)

    #commands needed
    #filler function
    def hello():
        print ("Hello")
    def doOpen():
        file=tkFileDialog.askopenfilename(parent=main,title="Open",mode='r')
        fileContents=file.read()
        main.textbox.delete(0.0,END)
        main.textbox.insert(0.0,fileContents)


    #creates menubar
    menubar=Menu(main)
    #creates file cascading menu
    filemenu=Menu(menubar, tearoff=0)
    filemenu.add_command(label="Open", command=doOpen)
    filemenu.add_command(label="Save", command=hello)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=hello)
    filemenu.add_command(label="About", command=hello)
    menubar.add_cascade(label="File", menu=filemenu)
    #creates edit cascading menu
    editmenu=Menu(menubar, tearoff=0)
    editmenu.add_command(label="Undo", command=hello)
    editmenu.add_command(label="Redo", command=hello)
    editmenu.add_command(label="Insert Image", command=hello)
    editmenu.add_command(label="Prefernces", command=hello)
    menubar.add_cascade(label="Edit", menu=editmenu)

    textbox.pack(expand=YES, fill=BOTH)
    main.config(menu=menubar)


    main.mainloop()

ERROR

Exception in Tkinter callback
Traceback (most recent call last):
File "Q:\PortablePython_1.1_py3.0.1\App\lib\tkinter\__init__.py", line 1399, in __call__
return self.func(*args)
TypeError: doOpen() takes exactly 1 positional argument (0 given)

3
Contributors
4
Replies
7
Views
6 Years
Discussion Span
Last Post by pyTony
0

well I don't have the filedialog files but the problem I see is the capital in doOpen either change it to doopen or just open but capitals have caused many a problem for me

0

In class you must allways have parameter when defining functions, which identifies the object, traditionally called self. Put those in place and post any other problems that may come!

Also calling own methods is done like: self.doOpen (consider changing Capitals to _ according standard style: self.do_open)

Edited by pyTony: n/a

0

Where would self be placed?

from tkinter import *
from tkinter.filedialog import LoadFileDialog
from tkinter.filedialog import SaveFileDialog
from tkinter.filedialog import Directory

class textEditor:

    def __init__(self):


        #main window
        self.main=Tk()
        self.main.title("Text Editor")
        self.textbox=Text(main)

        #commands needed
        #filler function
        def hello():
            print ("Hello")
        def open(self):
            file=tkFileDialog.askopenfilename(parent=main,title="Open",mode='r')
            fileContents=file.read()
            self.main.textbox.delete(0.0,END)
            self.main.textbox.insert(0.0,fileContents)


        #creates menubar
        menubar=Menu(main)
        #creates file cascading menu
        filemenu=Menu(menubar, tearoff=0)
        filemenu.add_command(label="Open", command=open)
        filemenu.add_command(label="Save", command=hello)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=hello)
        filemenu.add_command(label="About", command=hello)
        menubar.add_cascade(label="File", menu=filemenu)
        #creates edit cascading menu
        editmenu=Menu(menubar, tearoff=0)
        editmenu.add_command(label="Undo", command=hello)
        editmenu.add_command(label="Redo", command=hello)
        editmenu.add_command(label="Insert Image", command=hello)
        editmenu.add_command(label="Prefernces", command=hello)
        menubar.add_cascade(label="Edit", menu=editmenu)

        self.textbox.pack(expand=YES, fill=BOTH)
        self.main.config(menu=menubar)


        self.main.mainloop()

There is no result from this code (I assume it is an error with the package line).

Edited by Safia Abdalla: n/a

0
from tkinter import *
import tkinter.filedialog as tkfile

class textEditor(object):
    def __init__(self, main):
        #main window
        main.title("Text Editor")

        self.textbox=Text(main)
        self.textbox.pack(expand=YES, fill=BOTH)

        #creates menubar
        menubar=Menu(main)
        #creates file cascading menu
        filemenu=Menu(menubar, tearoff=0)
        filemenu.add_command(label="open", command=self.do_open)
        filemenu.add_command(label="Save", command=self.hello)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.hello)
        filemenu.add_command(label="About", command=self.hello)
        menubar.add_cascade(label="File", menu=filemenu)
        #creates edit cascading menu
        editmenu=Menu(menubar, tearoff=0)
        editmenu.add_command(label="Undo", command=self.hello)
        editmenu.add_command(label="Redo", command=self.hello)
        editmenu.add_command(label="Insert Image", command=self.hello)
        editmenu.add_command(label="Prefernces", command=self.hello)
        menubar.add_cascade(label="Edit", menu=editmenu)

        main.config(menu=menubar)
        self.main = main
        main.mainloop()

    #commands needed
    #filler function
    def hello(self):
        print ("self.hello")

    def do_open(self):
        file_contents = tkfile.askopenfile(parent=self.main, title="Open").read()
        self.textbox.delete(0.0,END)
        self.textbox.insert(0.0, file_contents )


app = textEditor(Tk())

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.