我有一个包含4列的表,这些列是从csv读取的,并使用tkinter显示。我希望在名为“Served”的列的每一行的末尾都有一个复选按钮。然而,我得到的不是复选按钮,而是“.!framed.!checkbut”。
下面是我的代码:
from tkinter import * from tkinter import ttk from ttkthemes import ThemedStyle import tkinter.font as font import csv root = Tk() root.title("A") #title shown in bar root.iconbitmap(r'C:/Users/...') #icon shown in bar TableMargin = ttk.Frame(root) TableMargin.grid(column=0, row=0, sticky=(N, W, E, S)) root.columnconfigure(0, weight=1) root.rowconfigure(0, weight=1) root.state('zoomed') #fullscreen # Setting Theme style = ThemedStyle(TableMargin) style.set_theme("black") #Heading label = ttk.Label(TableMargin, text='Orders') label['font'] = font.Font(size=20, weight="bold") label.grid(row=0, sticky=(N),pady=5) #Designing of Table style = ttk.Style() style.configure("Treeview.Heading", font=(None, 13)) # scrollbarx = Scrollbar(TableMargin, orient=HORIZONTAL) # scrollbary = Scrollbar(TableMargin, orient=VERTICAL) tree = ttk.Treeview(TableMargin, columns=("Table No.", "Order", "Time", "Served"), height=400, selectmode="extended") #, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set # scrollbary.config(command=tree.yview) # scrollbary.pack(side=RIGHT, fill=Y) # scrollbarx.config(command=tree.xview) # scrollbarx.pack(side=BOTTOM, fill=X) tree.heading('Table No.', text="Table No.", anchor=W) tree.heading('Order', text="Order", anchor=W) tree.heading('Time', text="Time", anchor=W) tree.heading('Served', text="Served", anchor=W) tree.column('#0', stretch=NO, minwidth=0, width=0) tree.column('#1', stretch=NO, minwidth=0, width=100) tree.column('#2', stretch=NO, minwidth=0, width=600) tree.column('#3', stretch=NO, minwidth=0, width=100) tree.column('#4', stretch=NO, minwidth=0, width=100) tree.grid(row=0, pady=50,padx=190,sticky=(N, E, S)) with open('C:/Users/...', encoding = "utf-8-sig") as f: reader = csv.DictReader(f, delimiter=',') for row in reader: table = row['Table'] order = row['Order'] time = row['Time'] var = IntVar() served = ttk.Checkbutton(TableMargin, text="", variable=var) served.grid() tree.insert("", 1, values=(table, order, time, served))
发布于 2019-12-19 00:41:11
您可以使用复选框的图像来完成此操作。首先,我使用Row布局将图像放在右侧:
style.layout('cb.Treeview.Row', [('Treeitem.row', {'sticky': 'nswe'}), ('Treeitem.image', {'side': 'right', 'sticky': 'e'})])
然后,我在每个树项目上使用“选中”和“取消选中”标签来控制复选框的图像(参见下面的代码)。
我还将项目的id作为标记添加到每个项目,以便可以将此标记绑定到按钮单击。当有点击时,我用 identify_column(event.x) 检查点击是否在“Served”列中,如果是这样,我就使用标签切换复选框的状态。
identify_column(event.x)
下面是完整的代码:
import tkinter as tk from tkinter import ttk root = tk.Tk() class CbTreeview(ttk.Treeview): def __init__(self, master=None, **kw): kw.setdefault('style', 'cb.Treeview') kw.setdefault('show', 'headings') # hide column #0 ttk.Treeview.__init__(self, master, **kw) # create checheckbox images self._im_checked = tk.PhotoImage('checked', data=b'GIF89a\x0e\x00\x0e\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x0e\x00\x0e\x00\x00\x02#\x04\x82\xa9v\xc8\xef\xdc\x83k\x9ap\xe5\xc4\x99S\x96l^\x83qZ\xd7\x8d$\xa8\xae\x99\x15Zl#\xd3\xa9"\x15\x00;', master=self) self._im_unchecked = tk.PhotoImage('unchecked', data=b'GIF89a\x0e\x00\x0e\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x0e\x00\x0e\x00\x00\x02\x1e\x04\x82\xa9v\xc1\xdf"|i\xc2j\x19\xce\x06q\xed|\xd2\xe7\x89%yZ^J\x85\x8d\xb2\x00\x05\x00;', master=self) style = ttk.Style(self) style.configure("cb.Treeview.Heading", font=(None, 13)) # put image on the right style.layout('cb.Treeview.Row', [('Treeitem.row', {'sticky': 'nswe'}), ('Treeitem.image', {'side': 'right', 'sticky': 'e'})]) # use tags to set the checkbox state self.tag_configure('checked', image='checked') self.tag_configure('unchecked', image='unchecked') def tag_add(self, item, tags): new_tags = tuple(self.item(item, 'tags')) + tuple(tags) self.item(item, tags=new_tags) def tag_remove(self, item, tag): tags = list(self.item(item, 'tags')) tags.remove(tag) self.item(item, tags=tags) def insert(self, parent, index, iid=None, **kw): item = ttk.Treeview.insert(self, parent, index, iid, **kw) self.tag_add(item, (item, 'unchecked')) self.tag_bind(item, '<ButtonRelease-1>', lambda event: self._on_click(event, item)) def _on_click(self, event, item): """Handle click on items.""" if self.identify_row(event.y) == item: if self.identify_column(event.x) == '#4': # click in 'Served' column # toggle checkbox image if self.tag_has('checked', item): self.tag_remove(item, 'checked') self.tag_add(item, ('unchecked',)) else: self.tag_remove(item, 'unchecked') self.tag_add(item, ('checked',)) tree = CbTreeview(root, columns=("Table No.", "Order", "Time", "Served"), height=400, selectmode="extended") tree.heading('Table No.', text="Table No.", anchor='w') tree.heading('Order', text="Order", anchor='w') tree.heading('Time', text="Time", anchor='w') tree.heading('Served', text="Served", anchor='w') tree.column('#1', stretch='no', minwidth=0, width=100) tree.column('#2', stretch='no', minwidth=0, width=600) tree.column('#3', stretch='no', minwidth=0, width=100) tree.column('#4', stretch='no', minwidth=0, width=70)