How should a scrollable spreadsheet be displayed within Tkinter?

一笑奈何 提交于 2019-12-11 04:13:14

问题


Currently I am using a Treeview. The problem is that I am using quite a large data set. So that the GUI isn't massive, I've limited the size of the Treeview to fit the window and added vertical and horizontal scrollbars. It displays the data exactly how I want, however there are speed issues when scrolling in each direction. Is there a better/faster way to display spreadsheet-like data.


回答1:


The below solution is cobbled together but should achieve the desired result:

from tkinter import *

class App:
    def __init__(self, root):
        self.entry = []
        self.sv = []
        self.root = root
        self.canvas = Canvas(self.root, background="#ffffff", borderwidth=0)
        self.frame = Frame(self.canvas, background="#ffffff")
        self.scrolly = Scrollbar(self.root, orient="vertical", command=self.canvas.yview)
        self.scrollx = Scrollbar(self.root, orient="horizontal", command=self.canvas.xview)
        self.canvas.configure(yscrollcommand=self.scrolly.set)#, xscrollcommand=self.scrollx.set)
        self.canvas.create_window((4,4), window=self.frame, anchor="nw", tags="self.frame")
        self.scrolly.pack(side="left", fill="y")
        self.canvas.pack(side="top", fill="both", expand=True)
        self.scrollx.pack(side="bottom", fill="x")
        self.frame.bind("<Configure>", self.onFrameConfigure)
        for i in range(15):
            self.entry.append([])
            self.sv.append([])
            for c in range(30):
                self.sv[i].append(StringVar())
                self.sv[i][c].trace("w", lambda name, index, mode, sv=self.sv[i][c], i=i, c=c: self.callback(sv, i, c))
                self.entry[i].append(Entry(self.frame, textvariable=self.sv[i][c]).grid(row=c, column=i))
    def onFrameConfigure(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))
    def callback(self, sv, column, row):
        print("Column: "+str(column)+", Row: "+str(row)+" = "+sv.get())

root = Tk()
App(root)
root.mainloop()



回答2:


I ended up using pandastable (https://github.com/dmnfarrell/pandastable). As it provided a quick and easy way of displaying data in a spreadsheet like manner. It also provides a lot of built in functionality, such as: sorting, filtering and applying functions to columns



来源:https://stackoverflow.com/questions/46403292/how-should-a-scrollable-spreadsheet-be-displayed-within-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!