-1

I am making this program. The idea is to save the time from a stopwatch to an excel sheet. But it would be better to save them all into one excel sheet. Right now it saves to multiple ones. I just can't think of a way to get all the stopwatches to save to the same sheet in different columns. So if anyone can think of a way, I would greatly appreciate it. I know I am probably missing something simple but I have to ask. I know it is not fully commented. The comments are really just short hand to remind me what the code does. if i missed a piece of info, you need that may help answering my question, just ask. Here is my code:

import pygtk, xlwt, pango, os
pygtk.require('2.0')
import gtk

import gobject
gobject.threads_init()

#inittial window
class mainTimer:
def new_timer(self, widget):
    new_Timer = Timers()


def destroy(self, widget, data=None):
    gtk.main_quit()

def __init__(self):
    self.timer_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.timer_window.set_position(gtk.WIN_POS_CENTER)
    self.timer_window.set_size_request(500,300)
    self.timer_window.set_title("Project Starter")
    self.button1 = gtk.Button("Pause")
    self.button1.connect("clicked", self.destroy)
    self.button1.set_tooltip_text("This will pause timer")

    self.button2 = gtk.Button("New Project Timer")
    self.button2.connect("clicked", self.new_timer)
    self.button2.set_tooltip_text("This will start a new project timer")

    fixed = gtk.Fixed()
    fixed.put(self.button1, 30, 250)
    fixed.put(self.button2, 90, 250)
    self.timer_window.add(fixed)
    self.timer_window.show_all()
    self.timer_window.connect("destroy", self.destroy)

def main(self):
    gtk.main()


class Timers:
def destroy(self, widget, data=None):
    self.gtk.main_quit()


def __init__(self):
    self.sec = 0
    self.min = 0
    self.hour = 0
    self.tg = 0
    self.total_time = (self.hour,self.min,self.sec)
    self.win()
    self.project_name()

#start window timer in thread
def start_t(self, widget):
    if self.tg == 0:
        self.tg = gobject.timeout_add(100, self.count)
#stop window timer
def stop_t(self, widget):
    if self.tg != 0:
        gobject.source_remove(self.tg)
        self.tg = 0
#Project Name pop up box
def project_name(self):
    self.projectName = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.projectName.set_position(gtk.WIN_POS_CENTER)
    self.projectName.set_size_request(280,160)
    self.text_entry = gtk.Entry()
    self.p_t = self.projectName.get_title()
    self.enter_button = gtk.Button("Enter")
    self.enter_button.connect("clicked", self.change_p_n)
    fixed = gtk.Fixed()
    fixed.put(self.text_entry, 50, 100)
    fixed.put(self.enter_button,210,100)
    self.projectName.add(fixed)
    self.projectName.show_all()
#change project name
def change_p_n(self,widget):
    self.timer_window.set_title(self.text_entry.get_text())
    self.projectName.destroy()

def save_box(self,widget):
    self.save = gtk.FileChooserDialog("Save As...", None,
    gtk.FILE_CHOOSER_ACTION_SAVE,
    (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
    gtk.STOCK_SAVE, gtk.RESPONSE_OK))
    self.save.set_default_response(gtk.RESPONSE_OK)
    self.save.set_current_name(self.timer_window.get_title())
    self.f_filter = gtk.FileFilter()
    self.f_filter.set_name("Save Times")
    self.f_filter.add_pattern("*")
    self.save.add_filter(self.f_filter)
    self.response = self.save.run()

    if self.response == gtk.RESPONSE_OK:
        self.wbk = xlwt.Workbook()
        self.sheet = self.wbk.add_sheet(self.timer_window.get_title())
        self.sheet.write(0,0,self.time1.get_text())
        self.sheet.write(0,1,self.time2.get_text())
        self.sheet.write(0,2,self.time3.get_text())
        self.wbk.save(self.save.get_filename())


#timer window
def win(self):
    self.timer_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.timer_window.set_position(gtk.WIN_POS_CENTER)
    self.timer_window.set_size_request(500,300)
    self.timer_window.set_title("Project Timers")
    self.button1 = gtk.Button("Pause")
    self.button1.set_tooltip_text("This will pause timer")
    self.button1.connect("clicked", self.stop_t)
    self.button2 = gtk.Button("Start")
    self.button2.set_tooltip_text("This will start timer")
    self.button2.connect("clicked", self.start_t)
    self.button3 = gtk.Button("Save")
    self.button3.set_tooltip_text("This will save your time to a excel file")
    self.button3.connect("clicked", self.save_box)
    self.time1 = gtk.Label("00")
    self.time1.modify_font(pango.FontDescription("40"))
    self.time2 = gtk.Label("00")
    self.time2.modify_font(pango.FontDescription("40"))
    self.time3 = gtk.Label("00")
    self.time3.modify_font(pango.FontDescription("30"))
    self.time4 = gtk.Label(":")
    self.time4.modify_font(pango.FontDescription("40"))
    self.time5 = gtk.Label(":")
    self.time5.modify_font(pango.FontDescription("40"))


    fixed = gtk.Fixed()
    fixed.put(self.button1, 30, 250)
    fixed.put(self.button2, 90, 250)
    fixed.put(self.button3, 150, 250)
    fixed.put(self.time1, 140, 100)
    fixed.put(self.time2, 215, 100)
    fixed.put(self.time3, 300, 110)
    fixed.put(self.time4, 280, 100)
    fixed.put(self.time5, 200, 100)
    self.timer_window.add(fixed)
    self.timer_window.show_all()


#timer count function
def count(self):
    self.sec += 1
    if self.sec >= 59:
        self.sec = 0
        self.min += 1
    if self.min >= 59:
        self.min = 0
        self.hour +=1

    self.time1.set_text(str(self.hour))
    self.time2.set_text(str(self.min))
    self.time3.set_text(str(self.sec))
    return True







if __name__ == "__main__":
timer1 = mainTimer()
timer1.main()
  • Instead of giving your entire program, it's a lot better to strip out all of the irrelevant stuff and provide the smallest example that demonstrates your problem. It's hard to find the relevant code within a huge example, and nobody can even run this to debug it unless they've got pygtk. That's probably why it took you 45 minutes to get an answer, and someone downvoted you in the meantime. See [SSCCE](http://sscce.org) for details. – abarnert May 08 '13 at 00:28

1 Answers1

0

First, is there a reason you need to save these to an .xls file instead of a .csv file? If you need features that CSV can't handle (formulas, graphs, etc.), obviously you need to go with a real spreadsheet, but for a bunch of columns of static data, CSV is a lot simpler, and Excel has no problem with it.

But assuming you do need to use an .xls file…


The reason it's creating a new file each time is that you're explicitly telling it to create a new file each time. Here's your code:

self.wbk = xlwt.Workbook()
self.sheet = self.wbk.add_sheet(self.timer_window.get_title())
self.sheet.write(0,0,self.time1.get_text())
self.sheet.write(0,1,self.time2.get_text())
self.sheet.write(0,2,self.time3.get_text())
self.wbk.save(self.save.get_filename())

This creates a new Workbook, adds a sheet, writes three cells to that sheet, and saves it. So, obviously, you get a whole new file.


If you don't want that, just don't create a new workbook and sheet each time. And you'll also need to keep track of the next row, so you don't keep overwriting the same cells over and over. And… that's it.

For example:

def __init__(self):
    # ... existing code
    self.wbk = xlwt.Workbook()
    self.sheet = self.wbk.add_sheet(self.timer_window.get_title())
    self.next_row = 0

def save_box(self,widget):
    # ... existing code
    if self.response == gtk.RESPONSE_OK:
        self.sheet.write(self.next_row, 0,self.time1.get_text())
        self.sheet.write(self.next_row, 1,self.time2.get_text())
        self.sheet.write(self.next_row, 2,self.time3.get_text())
        self.next_row += 1

Or, alternatively, instead of creating a new sheet each time, re-open the same sheet each time and add to the end of it.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Thank you that was the answer I was looking for. I'm sorry about posting all of my code. It is my first time asking for this kind of help on here. I will remember for next time. – user2360297 May 08 '13 at 20:42