How should a scrollable spreadsheet be displayed w

2019-08-09 11:54发布

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.

2条回答
老娘就宠你
2楼-- · 2019-08-09 12:34

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()
查看更多
趁早两清
3楼-- · 2019-08-09 12:39

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

查看更多
登录 后发表回答