chowchow
chowchow

Reputation: 23

GUI table with horizontal and vertical scrollbar (horizontal not properly working)

I am making a table using python for my assignment but the horizontal scrollbar is not working properly and the vertical scrollbar is not in the appropriate position.

I already made an n by m table in python with a horizontal and vertical scrollbars. The vertical scrollbar is working properly but the horizontal isn't. The table only displays 5 columns ans 7 rows, the remaining columns and rows can be viewed by using the scrolls.

My problem is that horizontal scrollbar extends up to the last column. Say, there are 10 columns, only 5 of them must be displayed first but the horizontal extends up to the last column. Moreover, the vertical scrollbar is not in the appropriate position.

The code below is an example .

# Create the Frame canvas
root = Tk()
frame_canvas = Frame(root)
frame_canvas.grid( row=2, column=2, pady=(5, 0), sticky='nw' )
frame_canvas.grid_rowconfigure( 0, weight=1 )
frame_canvas.grid_columnconfigure( 0, weight=1 )

frame_canvas.grid_propagate( False )

# Add a canvas in the frame
canvas = Canvas( frame_canvas)


# Link the scrollbars to the canvas
vsb = Scrollbar( frame_canvas, orient="vertical", command=canvas.yview )
canvas.configure( yscrollcommand=vsb.set )
vsb.grid( row=0, column=2, sticky='ns' )

hsb = Scrollbar(frame_canvas, orient="horizontal", command=canvas.xview())
hsb.grid(row=1, column=2, sticky='we')
canvas.configure( xscrollcommand=hsb.set )

canvas.grid( row=0, column=2, padx=(5, 5), sticky="news" )

# Create a frame to contain the cells of the table
frame_buttons = Frame(canvas)
canvas.create_window( (0, 0), window=frame_buttons, anchor='nw' )

# Add the contents to the table
header = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
prod_rows = 15
prod_col = len(header)
entries = [[Label() for j in xrange( prod_col )] for i in xrange( prod_rows )]

for i in range( 0, prod_rows ):
    if i == 0:
        for j in range( 0, prod_col ):
            entries[i][j] = Label(canvas, text=header[j], width=10 )
            entries[i][j].grid( row=i, column=j, sticky='news' )
    else:
        for j in range( 0, prod_col ):
            entries[i][j] = Label( frame_buttons, text=" ", relief="groove", width=10)  # ("%d,%d" % (i+1, j+1))
            entries[i][j].grid( row=i, column=j, sticky='news' )

# Update cell frames idle tasks to let tkinter calculate buttons sizes
frame_buttons.update_idletasks()

# Resize the canvas frame to show exactly a 5 by 7 table 
columns_width = sum( [entries[0][j].winfo_width() for j in range( 0, 5)] )
rows_height = sum( [entries[i][0].winfo_height() for i in range( 0, 8 )] )
frame_canvas.config( width=columns_width,
                 height=rows_height)     # + 
vsb.winfo_width()

# Set the canvas scrolling region
canvas.config(scrollregion=canvas.bbox( "all" ) )

root.mainloop()

this is an image of the output of the code above

The scrollbars must strictly be on the designated corner.

Upvotes: 1

Views: 823

Answers (1)

FrainBr33z3
FrainBr33z3

Reputation: 1105

For vsb:

  • It must be stuck to the extreme right of the column, i.e. use sticky = "nse"; or add another column to the "frame_canvas"
  • If the above doesn't work, use the option "rowspan" for vsb

For hsb:

  • Remove the parentheses after canvas.xview
  • Also, use sticky = "swe"

Upvotes: 1

Related Questions