I am trying to create an user interface that has both a scrolling section and and a non-scrolling section. In the scrolling section will be a list of labels and user entries and in the non-scroll-able section will be information on how to fill in the entries. I am thinking of using a separate window to display the information but was wondering whether it is possible to display both in the same window.
I have adapted the code from the response to this question: Adding a scrollbar to a group of widgets in Tkinter however when I try to add the message region and scrolling regions into separate frames the Python shell crashes and does not open. Here is the code I am working with.
import Tkinter as tk
labelList = []
for i in range(100):
labelList.append(["This is label %s " %(i),"99"])
class Example(tk.Frame):
#Creation of scrollable canvas
def __init__(self, root, labelList):
tk.Frame.__init__(self, root)
#Creating Scrollable and Non-scrollable frames
self.scrollFrame = tk.Frame(root,bg = "red")
self.scrollFrame.grid(row = 0, column = 0, rowspan = 3, columnspan = 2)
self.no_scrollFrame = tk.Frame(root,bg = "blue")
self.no_scrollFrame.grid(row = 0, column = 2, rowspan = 3, columnspan = 2)
#Canvas assigned to Scrollable Frame
self.canvas = tk.Canvas(self.scrollFrame, borderwidth=0, background="#ffffff")
self.frame = tk.Frame(self.canvas, background="#ffffff")
self.vsb = tk.Scrollbar(self.scrollFrame, 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, anchor="nw",
tags="self.frame")
#Message assigned to non-Scrollable Frame
messageText = """
Information \n
Information \n
Information \n
Information \n
Information \n
Information \n
Information \n
Information \n
"""
self.Message1 = tk.Message(self.no_scrollFrame, text=messageText)
self.Message1.grid(row = 1, rowspan = len(labelList)+1, column = 2)
self.frame.bind("<Configure>", self.onFrameConfigure)
self.entry ={}
self.label = {}
self.populate()
def populate(self):
i=0
if len(labelList) != 0:
for item in labelList:
e = tk.Entry(self.frame)
e.grid(row = i+1, column = 1)
self.entry[item[0]] = e
labelText = item[0] + " (" + item[1] + ") "
lb = tk.Label(self.frame,text=labelText)
lb.grid(row=i+1, column=0)
self.label[item[0]] = lb
i += 1
else:
master.destroy()
def onFrameConfigure(self, event):
'''Reset the scroll region to encompass the inner frame'''
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
if __name__ == "__main__":
root=tk.Tk()
Example(root,labelList).pack(side="top", fill="both", expand=True)
root.mainloop()