Python Forum

Full Version: unexpected text file output <_io.TextIOWrapper… encoding='cp1252'>
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When I run the following code I get the following output instead of the file's text:
List of children's names in the whatever group in alphabetical order: <_io.TextIOWrapper name='whatever.txt' mode='a' encoding='cp1252'>
from easygui import*

I run the script and using the buttonboxes I register children and create groups. I then add children to the groups using choiceboxes. The problem then arises when I use the 'List of children in each group' option and I don't know what's causing it nor how to fix it. Any help will be very much appreciated.

I'm using Pycharm on a Win7os.

from easygui import*


def register_child():
    enter1 = enterbox(msg="Enter the child's first name.", title=' ', default='',
                      strip=True, image=None, root=None)
    if enter1 is None:
        main()
        return
    else:
        while enter1 == "" or enter1.isalpha() == False:
            enter1 = enterbox(msg="Invalid entry. Please enter the child's first name using letters only.",
                              title=' ', default='', strip=True, image=None, root=None)

    enter2 = enterbox(msg="Enter the child's last name.", title=' ', default='',
                      strip=True, image=None, root=None)  # ask user to enter child's first name

    if enter2 is None:
        main()
        return
    else:
        while enter2 == "" or enter2.isalpha() == False:
            enter2 = enterbox(msg="Invalid entry. Please enter the child's last name using letters only.",
                              title=' ', default='', strip=True, image=None,
                              root=None)

    child_name = (enter1 + " " + enter2).title()
    file = open("Children.txt", 'a')
    file.write(child_name + "\n")
    file.close()

    register_more_child = ccbox("Would you like to register another child?", "Please choose an option.")
    if register_more_child == 1:
        register_child()
        return
    elif register_more_child == 0:
        main()
        return


def list_children():
    file = open("Children.txt", 'r')  # open the Children.txt file
    lineList = file.readlines()
    lineList.sort()  # sort the list alphabetically
    # display the list
    print("List of children's names in alphabetical order:")
    for line in lineList:
        print(line, end="")
    file.close()
    main()


def list_groups():
    file = open("Groups.txt", 'r')  # open the Children.txt file
    lineList = file.readlines()
    lineList.sort()  # sort the list alphabetically
    # display the list
    print("List of groups' names in alphabetical order:")
    for line in lineList:
        print(line, end="")
        file.close()
    main()
    return


def children_in_group():
    file = open("Groups.txt", 'r')
    lineList = [line.strip() for line in file]
    choice = choicebox("Which group would you like to display?", "Choose a group.",
                       choices=(lineList))
    file.close()

    file1 = open('%s.txt' % choice, 'r')
    line_list = file1.readlines()
    line_list.sort()  # sort the list alphabetically
    # display the list
    print("List of children's names in the " + choice + " group in alphabetical order:")
    for line in line_list:
        print(line, end="")
    file1.close()
    main()
    return


def add_child_to_group():
    file = open("Children.txt", 'r')  # open the Children.txt file
    lineList = file.readlines()
    file.close()
    # lineList.sort()
    choice1 = choicebox('Choose a child to enter into a group.', 'Add child to a group. ', choices=lineList)
    if choice1 is None:
        main()
        return

    else:
        file = open("Groups.txt", 'r')
        lineList = [line.strip() for line in file]

        choice2 = choicebox("Which group would you like to add the children to?", "Choose a group.",
                            choices=lineList)
        file.close()

        file1 = open('%s.txt' % choice2, 'a')
        file1.write(choice2 + "\n")
        file1.close()
        add_more_children_to_groups = ccbox("Would you like to add another child to a group?",
                                            "Please choose an option.")
        if add_more_children_to_groups == 1:
            add_child_to_group()
            return
        elif add_more_children_to_groups == 0:
            main()
            return


def create_group():
    enter = enterbox(msg="Enter the group's name.", title='Create Group ')
    file = open("Groups.txt", 'a')
    file.write(enter + "\n")
    file.close()

    file = open('%s.txt' % enter, 'w')
    file.close()

    more_groups = ccbox("Would you like to create another group?", "Please choose an option.")
    if more_groups == 1:
        create_group()
        return
    else:
        main()
        return


def children_menu():
    title2 = "Children."
    children_menu = ["1: Register a child.", "2: List of registered children."]
    button = buttonbox("Choose an option", title2, choices=children_menu)
    if button == children_menu[0]:
        register_child()
    if button == children_menu[1]:  # user chooses option 2 from the Children menu:
        list_children()


def group_menu():
    title3 = "Groups."
    groups_menu = ["1: Create a group.", "2: List of groups.", "3: Add a child to a group.",
                    "4: List of children in each group."]
    button = buttonbox("Choose an option", title3, choices=groups_menu)
    if button == groups_menu[0]:
        create_group()

    if button == groups_menu[1]:
        list_groups()

    if button == groups_menu[2]:
        add_child_to_group()

    if button == groups_menu[3]:
        children_in_group()


def main():
    title1 = "Main menu."
    main_menu = ["1: Children", "2: Groups", "3: Educators", "4: Tags", "5: Cancel"]
    button = buttonbox("Choose an option", title1, choices=main_menu)

    if button == main_menu[0]:
        children_menu()

    elif button == main_menu[1]:
        group_menu()

    elif button == main_menu[2]:
        title4 = "Educators."
        educator_menu = ["1: Register an educator.", "2: List of registered educators."]
        button = buttonbox("Choose an option", title4, choices=educator_menu)

    elif button == main_menu[3]:
        button = "4"
        title4 = "Tags."
        tags_menu = ["1: Create tags", "2: List of tags."]
        button = buttonbox("Choose an option", title4, choices=tags_menu)

    else:
        print("You cancelled.")
        return
main()
easygui is not a good choice for graphics programming as it doesn't support events.
Almost all GUI software will need to use events. Suggest you use the builtin tkinter, or
better yet wxpython.

What version of python are you running?
I'm running Python 3.6

I wanted to try tkinter but having a lot of trouble installing it.

So you think the problem is with the GUI?
If you are running Python 3.6, tkinter is already installed, it is a builtin. wxPython, however, you would have to install, though it's not difficult.