Skip to content Skip to sidebar Skip to footer

Scrollbar In Tkinter Grid

Possible Duplicate: Adding a scrollbar to a grid of widgets in Tkinter On my project, i want do display my results in a window, using Tkinter as GUI. I place them in a kind of t

Solution 1:

To adapt Bryan Oakley's answer to your specific problem:

  • create your frame with the canvas as parent
  • use the canvas as the parameter of scrollregion

Note: when subclassing in python, you do not need to store the result of parent __init__ since it operate on self.

Here is the patch:

def__init__(self,name): 
         self.name = name
-        self.frame=tk.Frame.__init__(self,root)

         if name=="BotWin":
+            self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
+            tk.Frame.__init__(self,self.canvas)
             tk.Label(self,text="FirstColBot",width=30).grid(row=0,column=0)            
             tk.Label(self,text="SecndColBot",width=20).grid(row=0,column=1)
-            self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
             self.vsb = tk.Scrollbar(root, orient="vertical", command=self.canvas.yview)
             self.canvas.configure(yscrollcommand=self.vsb.set)

             self.vsb.pack(side="right", fill="y")
             self.canvas.pack(side="left", fill="both", expand=True)
-            self.canvas.create_window((4,4), window=self.frame)
+            self.canvas.create_window((4,4), window=self)
             self.bind("<Configure>", self.OnFrameConfigure)

         elif name=="TopWin":
+            self.frame=tk.Frame.__init__(self,root)
             self.pack()
             tk.Label(self,text="FirstColTop",width=30).grid(row=0,column=0)         
             tk.Label(self,text="SecndColTop",width=20).grid(row=0,column=1)
@@ -41,7 +40,7 @@
         toprow+=1defOnFrameConfigure(self, event):
-        self.canvas.configure(scrollregion=self.frame.bbox("all"))
+        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

     defaddrowBot(self,stuff,otherstuff):
         global botrow

Solution 2:

From your question I'm pretty sure I can help you, I had a similar problem a few months ago, and I tried so many different solutions, in the end I found out about the ttk treeview widget, from the ttk module, which comes standard with python 2.7

here is a small example to help you see what I mean

from Tkinter import *
import ttk
root = Tk()
treedata = [('column 1', 'column 2'), ('column 1', 'column 2')]
column_names = ("heading1", "heading2")
tree = ttk.Treeview(root, columns = column_names, yscrollcommand = scrollbar.set)
scrollbar = ttk.Scrollbar(root)
scrollbar.pack(side = 'right', fill= Y)
for x in treedata:
    tree.insert('', 'end', values =x)
for col in column_names: 
    tree.heading(col, text = col.Title())
scrollbar.config(command=tree.yview)
tree.pack()

hope this helps there isn't a lot of docs on this, but google will help, one very helpful link: http://www.tkdocs.com/tutorial/tree.html

good luck :)

Post a Comment for "Scrollbar In Tkinter Grid"