python GUI ttk库 -- Apple的学习笔记里面我提到还要学习下treeview控件,已经入门并且找到了after方法可以进行周期更新。另外觉得布局不方便,今天找到了布局的解决方案,可以用frame一个个group打包,此时可以用pack。然后在frame内可以用grid进行各个子控件的位置处理。按这样的分类及分层管理,几百个控件的布局都没问题,哈哈~
做了个雏形,进行可行性调研摸底,还算是顺利!
动态效果
源码
from tkinter import *
from tkinter.ttk import *
from tkinter.messagebox import *
class IGBox():
"""use a ttk.TreeView as a multicolumn ListBox"""
def __init__(self,table_header = None,table_list = None):
self.table_header = table_header
self.table_list = table_list
self.tree = None
self.txtTag = []
self.varlist=[]
self._setup_widgets()
self._build_tree()
def _setup_widgets(self):
self.container = Frame()
self.container.pack(side=TOP,fill='both')
self.containerInfo = Frame()
self.containerInfo.pack(side=BOTTOM,fill=X) # fill X is important
# create a treeview with dual scrollbars
self.tree = Treeview(self.container,columns=self.table_header, show="headings")
self.tree.grid(column=0, row=0,padx=5)
self.vsb = Scrollbar(self.container,orient="vertical",
command=self.tree.yview)
self.hsb = Scrollbar(self.container,orient="horizontal",
command=self.tree.xview)
self.vsb.grid(column=1, row=0, sticky='ns')
self.hsb.grid(column=0, row=1, sticky='ew')
Button(self.containerInfo, text="send",width=5, command=self.update_table).grid(row=1, column=3, sticky='w')
self.checkVar = StringVar(value="0")
self.cycle = Checkbutton(self.containerInfo, text="cycle", variable=self.checkVar,command = self.cyclechange)
self.cycle.grid(row=1,column=5,sticky=W)
self.cyctimer = Entry(self.containerInfo, width=5)
self.cyctimer.grid(row=1, column=6, sticky=E,pady=5)
# label settings
for index, lbitem in enumerate(("ID","LEN","DATA")):
tempwidth = 5
if(lbitem == "DATA"):
tempwidth = 20
self.tag = Label(self.containerInfo,text=lbitem,width=tempwidth)
self.tag.grid(row=0,column=index,sticky=W,padx=5)
# text settings
for index in range(0,3):
tempwidth = 5
if(index == 2):
tempwidth = 20
self.txtTag.append("txt_" + str(index)) # save obj name to list
self.txtTag[index] = Entry(self.containerInfo,width=tempwidth)
self.txtTag[index].grid(row=1, column=index, sticky=E,pady=5,padx=5) # position
def cyclechange(self):
print(self.checkVar.get())
if(self.checkVar.get()=="1"):
self.get_data()
self.tree.insert('', 'end', values=(self.varlist))
self.varlist.clear()
self.tree.after(self.cyctimer.get(), self.cyclechange)
def _build_tree(self):
for col in self.table_header:
self.tree.heading(col, text=col.title())
self.tree.column(col, width=80)
self.tree.column('DATA', width=200)
for item in self.table_list:
self.tree.insert('', 'end', values=item)
def _rebuild_tree(self):
self.tree.configure(columns=self.table_header)
self._build_tree()
def get_data(self):
for index in range(0, 3):
var=self.txtTag[index].get()
print(var)
if(len(var)<=0):
showinfo(title='Info', message="Wrong input!")
return
else:
self.varlist.append(var)
def update_table(self):
self.get_data()
self.tree.insert('', 'end', values=(self.varlist))
self.varlist.clear()
if __name__ == "__main__" :
car_header = ['ID', 'LEN','DATA']
car_list = [
('0x100', '8',"01 02 03 04 05 06 07 08") ,
('0x111', '8',"11 22 33 44 55 66 77 88")
]
root = Tk()
root.title("IG Sim")
root.geometry('420x300')
IGBox(table_header=car_header,table_list=car_list)
root.mainloop()