Grace
Grace

Reputation: 11

Writing to a text file from Tkinter python

I am trying to take text from the user via tkinter and put it into a text file. I got the program to write something to the text file, but it is not what the user enters, but rather a bunch of random numbers and characters. Here is my code.

import Tkinter
from Tkinter import *

def writeFile (textObj):
    file = open("alaskaQuestion.txt",'a+')
    file.write(textFile2)
    textObj.insert(INSERT, file.read())
    file.close()

gui = Tkinter.Tk()


textFile2 = Tkinter.Entry(gui)
textFile2.grid(row=9, column=1)

textFile2 = str(textFile2)

buttonWrite = Tkinter.Button(gui, text = "Write To File", command = lambda: writeFile(textFile)).grid(row=8, column=1)

gui.mainloop()

Upvotes: 0

Views: 12314

Answers (2)

sh3r
sh3r

Reputation: 31

import tkinter
from tkinter import *

def writeFile():
    file = open('sh3rly.txt','a+')
    file.write(metinF.get() + '\n')
    file.close()

gui = Tk()

metinF = Entry(gui)
metinF.grid(row=9, column=1)

butonWrite = Button(gui)
butonWrite.config(text = 'Write To File', command = writeFile)
butonWrite.grid(row=8, column=1)

gui.mainloop()

This is new and true python 3.6.x CODE.

Upvotes: 3

rassar
rassar

Reputation: 5660

In order to get the contents of the entry, use file.write(textFile2).get().

Upvotes: 0

Related Questions