Tkinter vertical and horizontal scroll bars

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:

# Create root
self.root = Tk()
self.root.geometry('1000x500+0+0')

# Create canvas
self.canvas = Canvas(self.root)
self.canvas.pack(side=TOP, fill=BOTH, expand=TRUE)

# Create scrollbars
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)

# Attach canvas to scrollbars
self.canvas.configure(xscrollcommand=self.xscrollbar.set)
self.canvas.configure(yscrollcommand=self.yscrollbar.set)

# Create frame inside canvas
self.frame = Frame(self.canvas)
self.canvas.create_window((0,0), window=self.frame, anchor=NW)
self.frame.bind('<Configure>', self.set_scrollregion)

# Write db contents to canvas
self.print_dbcontents()

# Invoke main loop
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) # scrolling works in x but not y
self.canvas.pack(side=LEFT, fill=BOTH, expand=TRUE) # scrolling works in y but not x

I just need to get horizontal and vertical scrolling to work at the same time or find an alternative solution.

+9
source share
1 answer

, . , , , (, , ). , , , , " "?

:

self.canvas.pack(side=TOP, fill=BOTH, expand=TRUE)
self.xscrollbar.pack(side=BOTTOM, fill=X)
self.yscrollbar.pack(side=RIGHT, fill=Y)

, , . , , , . , , . , .

- , , . "" , /. -, , , , , . , fooobar.com/questions/1105115/... .

-, , . , , . , , :

  ||
==

, , :

   ||    -or-      ||
 ====            ==||

, import *, . import Tkinter as tk, tk "tk". (: tk.Canvas ..). , , ttk tkinter , , " *" . , , tk .

+11

All Articles