I am trying to dump the contents of a database into a Tkinter widget. There are enough rows and columns in the database where I need to have both horizontal and vertical scrollbars, but it's hard for me to work with horizontal and vertical scrolling. I am agnostic about which Tkinter widget is being used, but here is my current implementation:
self.root = Tk()
self.root.geometry('1000x500+0+0')
self.canvas = Canvas(self.root)
self.canvas.pack(side=TOP, fill=BOTH, expand=TRUE)
self.xscrollbar = Scrollbar(self.root, orient=HORIZONTAL, command=self.canvas.xview)
self.xscrollbar.pack(side=BOTTOM, fill=X)
self.yscrollbar = Scrollbar(self.root, orient=VERTICAL, command=self.canvas.yview)
self.yscrollbar.pack(side=RIGHT, fill=Y)
self.canvas.configure(xscrollcommand=self.xscrollbar.set)
self.canvas.configure(yscrollcommand=self.yscrollbar.set)
self.frame = Frame(self.canvas)
self.canvas.create_window((0,0), window=self.frame, anchor=NW)
self.frame.bind('<Configure>', self.set_scrollregion)
self.print_dbcontents()
self.root.mainloop()
def set_scrollregion(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox('all'))
In this implementation, I can make the scroll only work in one direction, depending on how I pack the canvas:
self.canvas.pack(side=TOP, fill=BOTH, expand=TRUE)
self.canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
I just need to get horizontal and vertical scrolling to work at the same time or find an alternative solution.
source
share