1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Rep Power

    [Python 3 + Tkinter] Text File not Saved, Error Found!

    Hey Python Programmers...

    I try to create an simple program, you can check the hobbys of the person...

    This is my script:
    from tkinter import *
    from tkinter import filedialog, Tk
    root = Tk()
    root.title('Hobby Checker')
    Hobbys = {'Jamie' : 'Natuurkunde', 'Ellister' : 'Darten', 'Sander' : 'Lezen', 'Coert' : 'Vrijwilliger', 'Rob' : 'Speelgoed', 'John' : 'Radio', 'Frank' : 'Televisie', 'Willie' : 'Kunst', 'Liesbeth' : 'Winkellen', 'Dylan' : 'Gamen', 'Marvin' : 'Voetbal', 'Mirian' : 'Paarden'}
    def SendName():
        Names = str(entry.get())
            string = str('{0} houd van {1}'.format(Names, Hobbys[Names]))
            if Names in Hobbys:
                listbox.insert(END, str(string))
                entry.delete(0, END)
            print('Sessie Mislukt, Naam niet Ondersteund!')
            entry.delete(0, END)
    def Export():
        folder = filedialog.asksaveasfilename(initialdir = 'C:/', filetypes = [('text files', '*.txt')])
        text2save = string.get(1.0, END)
        saved = open(folder, 'w')
        saved.write(text2save + '\n')
    scrollbar = Scrollbar(root)
    scrollbar.pack(side = RIGHT, fill = Y)
    listbox = Listbox(root, yscrollcommand = scrollbar.set)
    listbox.pack(side = TOP, fill = BOTH)
    scrollbar.config(command = listbox.yview)
    entry = Entry(root)
    entry.pack(side = BOTTOM, fill = X)
    sendbutton = Button(root, text = 'Send Name', command = SendName)
    sendbutton.pack(side = BOTTOM, fill = X)
    exportbutton = Button(root, text = 'Export', command = Export)
    exportbutton.pack(side = BOTTOM, fill = X)
    The means is thad you can typ an name, i generated in my tuple... than you must be
    can export them as an text file (*.txt) with the given file name (folder + name)...

    If i run my script, i get this error:
    Traceback (most recent call last):
    File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
    File "C:\Users\Gebruiker\Desktop\Python\Python Tkinter\Tkinter HobbyChecker.py", line 28, in Export
    text2save = string.get(1.0, END)
    NameError: name 'string' is not defined
    What is wrong with my code ?... it said thad the name string is not defined...
    I try to fix the problem of my empty document (my ducoment is empty) with the
    .get(1.0, END) function called in my string (string.get(1.0, END)), but this don't

    Can anyone help me to fix this problem ?.... i want to create an export tool, so you
    can export the for loop text ("Jamie houd van Natuurkunde", "Ellister houd van Darten", etc)
    thad i generate in my tuple.... (see above in this script)...

    The second problem is thad my exported document is empty, no txt file if you open the
    exported document...

    Can anyone help me to fix this problem, just i can learn about it ?...

    Thanks, Jamie.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Rep Power
    text2save = string.get(1.0, END)
    NameError: name 'string' is not defined

    it said thad the name string is not defined
    In order to use string.get() you have to have an Entry instance named string. You do not. Which is also why nothing is written to the file, i.e there is no Entry attached to string to get() from.
    Last edited by dwblas; March 10th, 2018 at 12:19 AM.

IMN logo majestic logo threadwatch logo seochat tools logo