Kent Kent - 1 year ago 69
Python Question

Tkinter python append lines in separate file

I have no formal programming experience so please forgive my lack of terminology and program structure. Stackoverflow has been a tremendous help. This is my first question so please be gentle.

I have been tasked with writing a GUI. As of now, I works well and I have over 3500 lines of code and multiple files..

I need the Append lines in a separate file if someone could please give me some guidance. Please let me know if my question is not clear enough. Thank you. (Python 2.7.x & Tkinter)

(this does not work for obvious reasons I just can't quite get my head around the class part)


from Tkinter import *

class Application(Frame):
def init(self, master=None):
Frame.init(self, master)

def createWidgets(self):
self.code = []

# Create Frames
self.FileFrame = Frame(self, bd=5)
self.FileFrame.grid(row=0, column=0, padx=10, sticky=N + S + E + W)

self.f10 = Label(self.FileFrame, text='Enter Number', width=15, font="-weight bold")
self.f10.grid(row=0, column=0)

self.entersomething = StringVar()
self.entersomething.set("123") = Entry(self.FileFrame, textvariable=self.entersomething, width=5), column=1)

self.Send = Button(self.FileFrame, text='Send To File', command=self.SendButton)
self.Send.grid(row=0, column=2,)

def SendButton(self):
f = open('c:\Python\code.txt', 'w')
for line in self.code:
f.write(line + '\n')

def DoIt(self):

thickness = float(

self.code = []

#something here to make it append the lines in

app = Application()

self.code.append('(Code Generated)')
self.code.append('#1=%.4f (Thickness)' % thickness)

Answer Source

You need to make one small change: Open the file with the 'a' flag, for append:

 f = open('c:\Python\code.txt', 'a')