亲爱的,我一直在尝试使用 Tkinter 在子窗口中插入树视图,但它非常复杂,因为我不知道如何将它们放在一起。非常感谢您!赫克托
这是我到目前为止所做的:
import Tkinter
from Tkinter import *
import tkFont
import ttk
root= Tk()
class McListBox(object):
def __init__(self):
self.tree = None
self._setup_widgets()
self._build_tree()
def _setup_widgets(self):
s = """
"""
msg = ttk.Label(wraplength="4i", justify="right", anchor="n",
padding=(6, 6, 6, 6, 6 ,6), text=s)
msg.pack(fill='x')
container = ttk.Frame()
container.pack(fill='both', expand=True)
self.tree = ttk.Treeview(columns=element_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 element_header:
self.tree.heading(col, text=col.title(),
command=lambda c=col: sortby(self.tree, c, 0))
self.tree.column(col, width=tkFont.Font().measure(col.title()))
for item in element_list:
self.tree.insert('', 'end', values=item)
for ix, val in enumerate(item):
col_w = tkFont.Font().measure(val)
if self.tree.column(element_header[ix], width=None) < col_w:
self.tree.column(element_header[ix], width=col_w)
def isnumeric(s):
for c in s:
if c in "0000123456789000-.":
numeric = True
else:
return False
return numeric
def change_numeric(data):
new_data = []
if isnumeric(data[0][0]):
for child, col in data:
new_data.append((float(child), col))
return new_data
return data
def sortby(tree, col, descending):
data = [(tree.set(child, col), child) for child in tree.get_children('')]
data = change_numeric(data)
data.sort(reverse=descending)
for ix, item in enumerate(data):
tree.move(item[1], '', ix)
tree.heading(col,
command=lambda col=col: sortby(tree, col, int(not descending)))
element_header = ["Device", "Type", "LETs Threshold (L0)"]
element_list = [('93L422', 'Bipolar', '0.6')]
mc_listbox = McListBox()
def Child_Window():
win2 = Toplevel()
message = "This is the child window"
Label(win2, text=message).pack()
Button(win2, text='OK', command=win2.destroy).pack()
Button(root, text='Bring up Message', command=Child_Window).pack()
root.mainloop()