如何在特定的时间间隔内刷新tkinter表(用于更新)?
规格: 蟒蛇3, Postgresql, psycopg2, 特金特 提前谢谢如何在特定的时间间隔内刷新tkinter表(用于更新)?,tkinter,python-3.5,Tkinter,Python 3.5,规格: 蟒蛇3, Postgresql, psycopg2, 特金特 提前谢谢 class MultiColumnListbox(object): """use a ttk.TreeView as a multicolumn ListBox""" def __init__(self): self.tree = None self._setup_widgets() self._build_tree() def _setup_widgets(self): s =
class MultiColumnListbox(object):
"""use a ttk.TreeView as a multicolumn ListBox"""
def __init__(self):
self.tree = None
self._setup_widgets()
self._build_tree()
def _setup_widgets(self):
s = """WOW CALL LIST"""
msg = ttk.Label(wraplength="4i", justify="left", anchor="n",
padding=(10, 2, 10, 6), text=s)
msg.pack(fill='x')
container = ttk.Frame()
container.pack(fill='both', expand=True)
# create a treeview with dual scrollbars
self.tree = ttk.Treeview(columns=car_header, show="headings")
vsb = ttk.Scrollbar(orient="vertical",
command=self.tree.yview)
hsb = ttk.Scrollbar(orient="horizontal",
command=self.tree.xview)
self.tree.configure(yscrollcommand=vsb.set,
xscrollcommand=hsb.set)
self.tree.grid(column=0, row=0, sticky='nsew', in_=container)
vsb.grid(column=1, row=0, sticky='ns', in_=container)
hsb.grid(column=0, row=1, sticky='ew', in_=container)
container.grid_columnconfigure(0, weight=1)
container.grid_rowconfigure(0, weight=1)
def _build_tree(self):
for col in car_header:
self.tree.heading(col, text=col.title(),
command=lambda c=col: sortby(self.tree, c, 0))
# adsjust the column's width to the header string
self.tree.column(col,
width=tkFont.Font().measure(col.title()))
for item in car_list:
self.tree.insert('', 'end', values=item)
# adjust column's width if necessary to fit each value
for ix, val in enumerate(item):
col_w = tkFont.Font().measure(val)
if self.tree.column(car_header[ix],width=None)<col_w:
self.tree.column(car_header[ix], width=col_w)
def sortby(tree, col, descending):
"""sort tree contents when a column header is clicked on"""
# grab values to sort
data = [(tree.set(child, col), child) \
for child in tree.get_children('')]
# if the data to be sorted is numeric change to float
#data = change_numeric(data)
# now sort the data in place
data.sort(reverse=descending)
for ix, item in enumerate(data):
tree.move(item[1], '', ix)
# switch the heading so it will sort in the opposite direction
tree.heading(col, command=lambda col=col: sortby(tree, col, \
int(not descending)))
# the test data ...
con = psycopg2.connect("dbname='wowcall' user='postgres' password='Admin@123'")
cur = con.cursor()
cur.execute("SELECT * FROM wowdata")
car_list = cur.fetchall()
car_header = ['WOW ID',' Agent Name','Customer No']
if __name__ == '__main__':
root = tk.Tk()
root.title("WOW Call")
listbox = MultiColumnListbox()
root.mainloop()
类多列列表框(对象):
“”“将ttk.TreeView用作多列列表框”“”
定义初始化(自):
self.tree=None
self.\u设置\u小部件()
self.\u构建树()
def_设置_小部件(自):
s=“”哇呼叫列表“”
msg=ttk.Label(wraplength=“4i”,justify=“left”,anchor=“n”,
padding=(10,2,10,6),text=s)
msg.pack(fill='x')
container=ttk.Frame()
container.pack(fill='both',expand=True)
#使用双滚动条创建树状视图
self.tree=ttk.Treeview(列=car\u标题,show=“标题”)
vsb=ttk.Scrollbar(orient=“vertical”,
command=self.tree.yview)
hsb=ttk.Scrollbar(orient=“horizontal”,
command=self.tree.xview)
self.tree.configure(yscrollcommand=vsb.set,
xscrollcommand=hsb.set)
self.tree.grid(column=0,row=0,sticky='nsew',在容器中)
网格(列=1,行=0,粘性=ns',在容器中)
网格(列=0,行=1,粘性=ew',在容器中)
container.grid\u column配置(0,权重=1)
container.grid_rowconfigure(0,权重=1)
定义构建树(自):
对于轿厢顶部的立柱:
self.tree.heading(col,text=col.title(),
command=lambda c=col:sortby(self.tree,c,0))
#仅将列的宽度添加到标题字符串
self.tree.column(col,
宽度=tkFont.Font().measure(col.title())
对于车辆列表中的项目:
self.tree.insert(“”,'end',value=item)
#如有必要,调整列的宽度以适应每个值
对于ix,枚举中的val(项目):
col_w=tkFont.Font().measure(val)
如果self.tree.column(car_header[ix],width=None)写一个刷新表的函数。在该函数中,让它在
之后用调用自己:
def refresh_table():
<do whatever you need to refresh the table>
# call again in 10 seconds
root.after(10000, refresh_table)
编写一个刷新表的函数。在该函数中,让它在
之后用调用自己:
def refresh_table():
<do whatever you need to refresh the table>
# call again in 10 seconds
root.after(10000, refresh_table)
我想知道你们是否必须告诉Tkinter不要更新屏幕,因为你们要更新视图中的数千条记录。然后在更新所有行后重新打开更新视图?@wineunuchs2unix:默认情况下会发生这种情况。Tkinter是单线程的,因此当您在更新窗口的过程中,窗口更改会被缓冲,直到控件返回到事件循环。然后,所有的屏幕更新都会同时发生。我想知道当你更新视图中的数千条记录时,你是否必须告诉Tkinter不要更新屏幕。然后在更新所有行后重新打开更新视图?@wineunuchs2unix:默认情况下会发生这种情况。Tkinter是单线程的,因此当您在更新窗口的过程中,窗口更改会被缓冲,直到控件返回到事件循环。然后,所有屏幕更新一次发生。