填充行时更改 Excel 文件中的行
Changing row in Excel file when row is filled
我正在使用 Tkinter 编写一个简单的计时器。当在 Excel 文件中激活停止按钮时,我希望我的代码保存计时器显示的时间。计时器工作正常,但数据仅存储在 Excel 文件的一行中。例如,我希望它改变行。连续 5 个单元格被填充。
我一直在尝试使用条件 for
循环。例如。每次单元格值为 not None
时,我都尝试将 min_row
增加 1,但它不起作用。
有人能告诉我我做错了什么吗?
# ***** IMPORTS *****
import tkinter as tk
from openpyxl import Workbook
from openpyxl import load_workbook
running = False
# time variables initially set to 0
hours, minutes, seconds, total_time = 0, 0, 0, 0
def start():
global running
if not running:
update()
running = True
# pause function
def pause():
global running
if running:
# cancel updating of time using after_cancel()
stopwatch_label.after_cancel(update_time)
running = False
# reset function
def reset():
global running
if running:
# cancel updating of time using after_cancel()
stopwatch_label.after_cancel(update_time)
running = False
# set variables back to zero
global hours, minutes, seconds
hours, minutes, seconds = 0, 0, 0
# set label back to zero
stopwatch_label.config(text='00:00:00')
# update stopwatch function
def update():
# update seconds with (addition) compound assignment operator
global hours, minutes, seconds, total_time
seconds += 1
total_time += 1
if seconds == 60:
minutes += 1
seconds = 0
if minutes == 60:
hours += 1
minutes = 0
# format time to include leading zeros
hours_string = f'{hours}' if hours > 9 else f'0{hours}'
minutes_string = f'{minutes}' if minutes > 9 else f'0{minutes}'
seconds_string = f'{seconds}' if seconds > 9 else f'0{seconds}'
# update timer label after 1000 ms (1 second)
stopwatch_label.config(text=hours_string + ':' + minutes_string + ':' + seconds_string)
global update_time
update_time = stopwatch_label.after(1000, update)
def excel_update():
dest_filename = r"C:\Users\abbas\OneDrive\Desktop\Prog\Flex\tider.xlsx"
#dest_filename = r"C:\Users\abbas\OneDrive\Desktop\Prog\Flex\test.xlsx"
wb = load_workbook(dest_filename)
ws = wb.active
next_row = 1
cell_range = ws.iter_rows(min_row=next_row, max_row=3, max_col=2)
#cell_range = ws["A1":"B3"]
for rows in cell_range:
for columns in rows:
if columns.value[columns] is None:
columns.value[columns] = total_time
break
wb.save(dest_filename)
# ***** WIDGETS *****
# create main window
root = tk.Tk()
#root.geometry('450x200')
root.resizable(width=False, height=False)
root.title('Stopwatch')
# label to display time
stopwatch_label = tk.Label(text='00:00:00', font=('Arial', 20))
stopwatch_label.pack()
# start, pause, reset, stop buttons
start_button = tk.Button(text='start', height=3, width=5, font=('Arial', 10), command=start)
start_button.pack(side=tk.LEFT)
stop_button = tk.Button(text='stop', height=3, width=5, font=('Arial', 10), command=lambda:[root.quit(), excel_update()])
stop_button.pack(side=tk.LEFT)
pause_button = tk.Button(text='pause', height=3, width=5, font=('Arial', 10), command=pause)
pause_button.pack(side=tk.LEFT)
reset_button = tk.Button(text='reset', height=3, width=5, font=('Arial', 10), command=reset)
reset_button.pack(side=tk.LEFT)
# ***** MAINLOOP *****
root.mainloop()
根据您更新后的问题中的代码,我认为如下所示更改 excel_update()
函数将使它执行您想要的操作。
它搜索从 min_row
到 max_row
的行的列以查找空的 cell
。如果找到一个,它会为其分配时间值并引发 StopIteration
异常以跳出嵌套的 for
循环并终止搜索。然后简单地忽略异常,因为它是正常处理的一部分。
from tkinter.messagebox import showerror
def excel_update():
def excel_update():
dest_filename = r"C:\Users\abbas\OneDrive\Desktop\Prog\Flex\tider.xlsx"
wb = load_workbook(dest_filename)
ws = wb.active
# Put value in next empty cell in specified region (if one exists).
try:
for row in ws.iter_rows(min_row=1, max_row=3, max_col=3):
for cell in row:
if cell.value is None:
cell.value = total_time
raise StopIteration
else: # No exception raised.
showerror('Error!', 'No empty cells left for storing timer time!')
except StopIteration:
pass
wb.save(dest_filename) # Update file.
我正在使用 Tkinter 编写一个简单的计时器。当在 Excel 文件中激活停止按钮时,我希望我的代码保存计时器显示的时间。计时器工作正常,但数据仅存储在 Excel 文件的一行中。例如,我希望它改变行。连续 5 个单元格被填充。
我一直在尝试使用条件 for
循环。例如。每次单元格值为 not None
时,我都尝试将 min_row
增加 1,但它不起作用。
有人能告诉我我做错了什么吗?
# ***** IMPORTS *****
import tkinter as tk
from openpyxl import Workbook
from openpyxl import load_workbook
running = False
# time variables initially set to 0
hours, minutes, seconds, total_time = 0, 0, 0, 0
def start():
global running
if not running:
update()
running = True
# pause function
def pause():
global running
if running:
# cancel updating of time using after_cancel()
stopwatch_label.after_cancel(update_time)
running = False
# reset function
def reset():
global running
if running:
# cancel updating of time using after_cancel()
stopwatch_label.after_cancel(update_time)
running = False
# set variables back to zero
global hours, minutes, seconds
hours, minutes, seconds = 0, 0, 0
# set label back to zero
stopwatch_label.config(text='00:00:00')
# update stopwatch function
def update():
# update seconds with (addition) compound assignment operator
global hours, minutes, seconds, total_time
seconds += 1
total_time += 1
if seconds == 60:
minutes += 1
seconds = 0
if minutes == 60:
hours += 1
minutes = 0
# format time to include leading zeros
hours_string = f'{hours}' if hours > 9 else f'0{hours}'
minutes_string = f'{minutes}' if minutes > 9 else f'0{minutes}'
seconds_string = f'{seconds}' if seconds > 9 else f'0{seconds}'
# update timer label after 1000 ms (1 second)
stopwatch_label.config(text=hours_string + ':' + minutes_string + ':' + seconds_string)
global update_time
update_time = stopwatch_label.after(1000, update)
def excel_update():
dest_filename = r"C:\Users\abbas\OneDrive\Desktop\Prog\Flex\tider.xlsx"
#dest_filename = r"C:\Users\abbas\OneDrive\Desktop\Prog\Flex\test.xlsx"
wb = load_workbook(dest_filename)
ws = wb.active
next_row = 1
cell_range = ws.iter_rows(min_row=next_row, max_row=3, max_col=2)
#cell_range = ws["A1":"B3"]
for rows in cell_range:
for columns in rows:
if columns.value[columns] is None:
columns.value[columns] = total_time
break
wb.save(dest_filename)
# ***** WIDGETS *****
# create main window
root = tk.Tk()
#root.geometry('450x200')
root.resizable(width=False, height=False)
root.title('Stopwatch')
# label to display time
stopwatch_label = tk.Label(text='00:00:00', font=('Arial', 20))
stopwatch_label.pack()
# start, pause, reset, stop buttons
start_button = tk.Button(text='start', height=3, width=5, font=('Arial', 10), command=start)
start_button.pack(side=tk.LEFT)
stop_button = tk.Button(text='stop', height=3, width=5, font=('Arial', 10), command=lambda:[root.quit(), excel_update()])
stop_button.pack(side=tk.LEFT)
pause_button = tk.Button(text='pause', height=3, width=5, font=('Arial', 10), command=pause)
pause_button.pack(side=tk.LEFT)
reset_button = tk.Button(text='reset', height=3, width=5, font=('Arial', 10), command=reset)
reset_button.pack(side=tk.LEFT)
# ***** MAINLOOP *****
root.mainloop()
根据您更新后的问题中的代码,我认为如下所示更改 excel_update()
函数将使它执行您想要的操作。
它搜索从 min_row
到 max_row
的行的列以查找空的 cell
。如果找到一个,它会为其分配时间值并引发 StopIteration
异常以跳出嵌套的 for
循环并终止搜索。然后简单地忽略异常,因为它是正常处理的一部分。
from tkinter.messagebox import showerror
def excel_update():
def excel_update():
dest_filename = r"C:\Users\abbas\OneDrive\Desktop\Prog\Flex\tider.xlsx"
wb = load_workbook(dest_filename)
ws = wb.active
# Put value in next empty cell in specified region (if one exists).
try:
for row in ws.iter_rows(min_row=1, max_row=3, max_col=3):
for cell in row:
if cell.value is None:
cell.value = total_time
raise StopIteration
else: # No exception raised.
showerror('Error!', 'No empty cells left for storing timer time!')
except StopIteration:
pass
wb.save(dest_filename) # Update file.