0

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()
1
  • you are trying to write a widget, not the contents of the widget. Getting the contents from widgets is covered in probably every tkinter tutorial and documentation site. Commented Oct 12, 2016 at 21:40

2 Answers 2

3
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.