我试图用网格显示一个表,该表是270行70列 显然太大了,无法一次显示所有照片。 如果将行数限制为50,则需要12秒,当然,随着时间的增加,行数会更多,然后在某个时候它会变得很奇怪。 是否要先设置例如20行的标签数据,然后进行显示,然后再设置下20行,将其添加到显示中,依此类推?
def populate_using_tkinter(frame, project_list):
current_row_number = 0;
# Columns ********************************************************************************************************
label_id_column = Tkinter.Label(frame, text=Constants.PROJECT_COLUMN_TITLE_Id, borderwidth=GRID_BORDER_WIDTH, relief="solid", background=row_header_color)
<more columns>
label_ContractorID_column = Tkinter.Label(frame,text = Constants.PROJECT_COLUMN_TITLE_ContractorID, borderwidth = GRID_BORDER_WIDTH,relief = "solid")
label_id_column.grid(row=current_row_number, column=Constants.PROJECT_ID_COLUMN, sticky="nsew")
<more columns>
label_ContractorID_column.grid(row=current_row_number, column=Constants.PROJECT_ContractorID_COLUMN, sticky="nsew")
current_row_number +=1
frame.rowconfigure(current_row_number, minsize=row_height, weight=0)
for project in project_list:
project_id = project[0]
<more assignments>
project_ContractorID = project[70]
# Data ********************************************************************************************************
try:
label_project_id = Tkinter.Label(frame, text=project_id, borderwidth=GRID_BORDER_WIDTH, relief="solid", background=row_color)
<more data>
label_project_ContractorID=Tkinter.Label(frame,text=projectContractorID,anchor="w",borderwidth=GRID_BORDER_WIDTH,relief="solid", background=row_color)
label_project_id.grid(row=current_row_number, column=Constants.PROJECT_ID_COLUMN, sticky="nsew")
<more data>
label_project_ContractorID.grid(row=current_row_number, column=Constants.PROJECT_ContractorID_COLUMN, sticky="nsew")
except IndexError:
print ("[ERROR] IndexError: " + str(current_row_number) )
current_row_number +=1
def main():
project_list = <get from database> # 270 x 70
root = Tkinter.Tk()
canvas = Tkinter.Canvas(root, borderwidth=0, background="#ffffff")
frame = Tkinter.Frame(canvas, background="#ffffff")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
populate_using_tkinter(frame, project_list)
root.geometry("1000x600")
root.mainloop()
答案 0 :(得分:1)
这听起来像是更适合treeview
小部件的工作。但是,如果要改为使用标签表,则一种方法是将生成器与after
方法结合使用:
import tkinter as tk
root = tk.Tk()
data = {i:i for i in range(200)}
def display_data():
row = 0
for k,v in data.items():
tk.Label(root,text=k).grid(row=row,column=0)
tk.Label(root,text=v).grid(row=row,column=1)
row+=1
if not row % 5: #configure this to display the number of rows each second
root.after(1000, fetch)
yield
a = display_data()
def fetch():
try:
next(a)
except StopIteration:
return
root.after(1000, fetch)
root.mainloop()
或者您也可以将其放在treeview
中。通常,我发现这更易于显示数据,并且它还提供了许多方便的方法,使您可以进行排序,导出等。我还添加了垂直和水平滚动条以供参考。
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
class TreeFrame(tk.Frame):
def __init__(self,master,**kwargs):
tk.Frame.__init__(self,master,**kwargs)
self.tree = ttk.Treeview(self, selectmode='extended', height=24)
self.tree.grid(row=0,column=0,sticky="nsew")
vsb = ttk.Scrollbar(self, orient="vertical",command=self.tree.yview)
vsb.grid(row=0,column=1,sticky="ns")
hsb = ttk.Scrollbar(self, orient="horizontal",command=self.tree.xview)
hsb.grid(row=1,column=0,sticky="ew")
self.tree.configure(yscrollcommand=vsb.set)
self.tree.configure(xscrollcommand=hsb.set)
header = [i for i in range(70)]
self.tree["columns"] = header
self.tree['show'] = 'headings'
self.start = 0
for i in range(len(header)):
self.tree.column(header[i], width=15, minwidth=27, anchor="w")
self.tree.heading(header[i], text=header[i], anchor='w')
def dummy_data(self):
for i in range(self.start, self.start+20):
current = self.tree.insert("","end",values=[i for _ in range(70)])
self.tree.see(current)
self.start += 20
if self.start >= 270:
return
else:
root.after(1000,self.dummy_data)
tree_frame = TreeFrame(root)
tree_frame.pack()
tk.Button(root,text="Add dummy data",command=tree_frame.dummy_data).pack()
root.mainloop()