Python Tkinter,如果从CSV文件中删除了值,则删除冗余行
我试图在Tkinter中构建一个生产调度GUI。 一段单独的代码将访问我的生产计划,并将数据粘贴到csv文件中。随着作业在调度程序中完成,csv数据中的列表将变小。随着更多作业的添加,csv数据中的列表将变长。 问题在于,当列表变长时,会产生一定数量的条目。从csv文件中删除条目时,标签不会刷新以显示较少的条目 如果我关闭tk接口并重新打开它,它会显示正确的信息,因此我知道我需要刷新标签,但是使用label.destroy()不能做到这一点。我试图使csv_标签成为一个全局标签,这样它就可以在我的刷新窗口函数中直接引用,这样它就可以销毁标签的确切实例,但这似乎不起作用。我使用destroy正确吗Python Tkinter,如果从CSV文件中删除了值,则删除冗余行,python,csv,tkinter,iteration,Python,Csv,Tkinter,Iteration,我试图在Tkinter中构建一个生产调度GUI。 一段单独的代码将访问我的生产计划,并将数据粘贴到csv文件中。随着作业在调度程序中完成,csv数据中的列表将变小。随着更多作业的添加,csv数据中的列表将变长。 问题在于,当列表变长时,会产生一定数量的条目。从csv文件中删除条目时,标签不会刷新以显示较少的条目 如果我关闭tk接口并重新打开它,它会显示正确的信息,因此我知道我需要刷新标签,但是使用label.destroy()不能做到这一点。我试图使csv_标签成为一个全局标签,这样它就可以在我
# import the required modules
import tkinter as tk
import datetime
import time
import threading
import csv
#create the tk interface window
window = tk.Tk()
window.title("Test")
window.geometry('1024x768+-7+0')
#create a clock label to show current time
clock_label = tk.Label(window)
clock_label.grid(column=1, row=0)
#clock function to get current time
def clock():
time = datetime.datetime.now().strftime("Time: %I:%M:%S %p")
clock_label.config(text=time)
window.after(1000, clock)
#csv function which gets info from test.csv and pastes it into the label, situated on the window
def paste_csv():
with open("test.csv", newline="") as file:
reader = csv.reader(file)
r = 0
for col in reader:
c = 0
for row in col:
# i've added some styling
global csv_label
csv_label = tk.Label(window, width=30, height=2, \
text=row, relief=tk.RIDGE, bg="white")
csv_label.grid(row=r, column=c)
c += 1
r += 1
#attempt at destroying the label from the window
def refresh_window():
csv_label.destroy()
#at every second, update the csv data on the window
def paste(delay):
next_time = time.time() + delay
while True:
time.sleep(max(1, next_time - time.time()))
try:
paste_csv()
except:
print("tick1")
#at every 5 seconds, refresh the data on the window to reflect changes
def refresh(delay):
next_time = time.time() + delay
while True:
time.sleep(max(4, next_time - time.time()))
try:
refresh_window()
except:
print("tick2")
#Thread initiations
threading.Thread(target=lambda: paste(1)).start()
threading.Thread(target=lambda: refresh(5)).start()
#autodisplay clock and csv data on startup
clock()
paste_csv()
#main window loop
window.mainloop()
插入标签和刷新时不需要使用线程。在()之后使用
就足够了。要删除冗余标签,您需要有一个包含已创建标签的列表,并在创建新标签之前使用此列表删除现有标签:
# import the required modules
import tkinter as tk
import csv
import datetime
# hold the csv labels
csv_label_list = []
#create the tk interface window
window = tk.Tk()
window.title("Test")
window.geometry('1024x768+0+0')
#create a clock label to show current time
clock_label = tk.Label(window)
clock_label.grid(column=1, row=0)
#clock function to update current time
def clock():
time = datetime.datetime.now().strftime("Time: %I:%M:%S %p")
clock_label.config(text=time)
window.after(1000, clock)
#csv function which gets info from test.csv and pastes it into the label, situated on the window
def paste_csv():
with open("test.csv", newline="") as file:
# remove existing labels
for lbl in csv_label_list:
lbl.destroy()
csv_label_list.clear()
# create new labels
reader = csv.reader(file)
r = 0
for col in reader:
c = 0
for row in col:
# i've added some styling
csv_label = tk.Label(window, width=30, height=2,
text=row, relief=tk.RIDGE, bg="white")
csv_label.grid(row=r, column=c)
csv_label_list.append(csv_label)
c += 1
r += 1
# update every 5 seconds
window.after(5000, paste_csv)
#autodisplay clock and csv data on startup
clock()
paste_csv()
#main window loop
window.mainloop()
*请注意,如果CSV文件的第一行中有多个列,则时钟将被新标签覆盖。
import*
通常是不好的做法,也是如此,例外情况除外
@AMC,谢谢提醒,将使它们不在我的最终tk应用程序中。删除所有行和列并只创建所需的行更容易。但这意味着您必须保留某个列表中的所有元素才能访问它们并销毁它们。或者在<代码>框架> />代码中创建条目,以后必须“代码>销毁())/>代码>只有<代码>框架> >您也可以考虑使用<代码> TTK。Treeview < /代码>如果您只想显示您的信息。您可以通过“代码>根目录。GRIDSLaveSe())/代码>返回的所有小部件,如果是<代码>标签<代码>,则调用<代码>销毁()。