-1

我正在制作这个程序。这个想法是为了节省从秒表到 Excel 表的时间。但最好将它们全部保存到一张excel表中。现在它保存到多个。我只是想不出一种方法让所有秒表保存到不同列的同一张表中。因此,如果有人能想到一种方法,我将不胜感激。我知道我可能遗漏了一些简单的东西,但我不得不问。我知道它没有完全评论。这些评论实际上只是提醒我代码的作用的简写。如果我错过了一条信息,您需要这可能有助于回答我的问题,请问。这是我的代码:

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()
4

1 回答 1

0

首先,是否有理由需要将这些保存到 .xls 文件而不是 .csv 文件?如果你需要CSV无法处理的功能(公式、图表等),显然你需要使用真正的电子表格,但是对于一堆静态数据的列,CSV要简单得多,Excel也没有问题用它。

但假设您确实需要使用 .xls 文件……</p>


它每次都创建一个新文件的原因是您明确告诉它每次都创建一个新文件。这是你的代码:

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())

这将创建一个新的Workbook,添加一个工作表,将三个单元格写入该工作表,然后保存它。所以,很明显,你得到了一个全新的文件。


如果您不希望这样,请不要每次都创建新的工作簿和工作表。而且您还需要跟踪下一行,这样您就不会一遍又一遍地覆盖相同的单元格。而且……就是这样。

例如:

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

或者,或者,不是每次都创建一个新工作表,而是每次重新打开同一个工作表并添加到它的末尾。

于 2013-05-08T00:16:46.973 回答