Python Tkinter scrollbar canvas not working

365 views Asked by At

I'm having trouble getting a scrollbar to work for a tkinter frame. The frame i want to be able to scroll is a grid made up of imported SQL content. I have contained this frame in a canvas (as suggested in other posts).

When I load the particular screen, the canvas loads in an incredibly small space, with a couple of the SQL content labels and the scrollbar behind them. When the scrollbar is used, the canvas expands to fill the appropriate size and it works without any issue.

Here are a couple of screenshots, both before and after the scrollbar is used:

before scroll used:

before scroll used

after scroll used:

after scroll used

And below is the code for this particular frame:

def drawBoardMain(): #BOARDS
global displayName
global body
global root
global mainFont

body=Frame(root)

headFrame=Frame(body) #heading

logoSource=PhotoImage(file='holcombe.gif')
logo=Label(headFrame,image=logoSource)
logo.logoSource=logoSource
logo.grid(row=0,rowspan=2,column=0)

heading=Label(headFrame,text='Display Management System - Board Management',font=(mainFont,24)).grid(row=0,rowspan=2,column=1)

loginLabel=Label(headFrame,text='Logged in as:',font=(mainFont,12)).grid(row=0,column=2,sticky=S)

userLabel=Label(headFrame,text=displayName,font=(mainFont,12)).grid(row=1,column=2,sticky=N)

headFrame.pack() #heading

mainFrame=Frame(body) #main body

introLabel=Label(mainFrame,text='Select a board to view more details or make changes, or click the button below to add a new one',font=(mainFont,14)).grid(column=0,row=0)

containingCanvas=Canvas(mainFrame,height=500,width=900) #canvasOPEN

myscrollbar=Scrollbar(containingCanvas,orient="vertical",command=containingCanvas.yview) #scrollbarOPEN

containingCanvas.configure(yscrollcommand=myscrollbar.set)
containingCanvas.configure(scrollregion = containingCanvas.bbox("all"))
myscrollbar.pack(fill='y',side=RIGHT) #scrollbarCLOSE

containingCanvas.grid(column=0,row=1,pady=15) #canvasCLOSE

users=Frame(containingCanvas) #board browserOPEN

con=sql.connect('datastore.db')
cur=con.cursor()
cur.execute('SELECT boardID FROM tblBoards')
boards=cur.fetchall()
cur.execute('SELECT locationDescription FROM tblBoards')
locationDescriptions=cur.fetchall()
cur.execute('SELECT department FROM tblBoards')
departments=cur.fetchall()
cur.execute('SELECT comments FROM tblBoards')
comments=cur.fetchall()
con.close()

for i in range(len(boards)):
    a=Button(users,text=boards[i],font=(mainFont,14),width=20,padx=3,command=lambda x=boards[i][0]:handleBoardEdit(x))
    a.grid(column=0,row=i+1,sticky=NSEW,padx=1)
for j in range(len(locationDescriptions)):
    b=Label(users,text=locationDescriptions[j],font=(mainFont,14),padx=3,relief=RAISED)
    b.grid(row=j+1,column=1,sticky=NSEW,padx=1)
for k in range(len(departments)):
    c=Label(users,text=departments[k],font=(mainFont,14),padx=3,relief=RAISED)
    c.grid(row=k+1,column=2,sticky=NSEW,padx=1)
for l in range(len(comments)):
    d=Label(users,text=comments[l],font=(mainFont,14),padx=3,relief=RAISED)
    d.grid(row=l+1,column=3,sticky=NSEW,padx=1)

uLabel=Label(users,text="Board ID",font=(mainFont,16),padx=3,relief=RAISED).grid(column=0,row=0,sticky=NSEW)
fLabel=Label(users,text="Location",font=(mainFont,16),padx=3,relief=RAISED).grid(column=1,row=0,sticky=NSEW)
sLabel=Label(users,text="Department",font=(mainFont,16),padx=3,relief=RAISED).grid(column=2,row=0,sticky=NSEW)
rLabel=Label(users,text="Comments",font=(mainFont,16),padx=3,relief=RAISED).grid(column=3,row=0,sticky=NSEW)

users.bind("<Configure>", lambda event, containingCanvas=containingCanvas: containingCanvas.configure(scrollregion=containingCanvas.bbox("all")))
users.pack(fill='both',side=LEFT) #board browserCLOSE    
containingCanvas.create_window((4,4),window=users, anchor="nw")


addButton=Button(mainFrame,text='Add New',font=(mainFont,14),command=handleAddBoard).grid(column=0,row=2)

mainFrame.pack() #main body

footFrame=Frame(body) #footer

logoutButton=Button(footFrame,text='Back to Menu',width='7',height='2',wraplength='80',font=(mainFont,14),command=returnMainHandle).grid(column=0,row=0,pady=15)

footFrame.pack(side=BOTTOM) #footer

body.pack(fill=BOTH,expand=1)

I reckon I've probably just got something in the wrong order. Any help is appreciated.

I know this code is terribly inefficient and I would probably be better-off creating a solution using objects, however this is unviable at this point so procedural answers only please thanks :)

0

There are 0 answers