具有水平和垂直滚动条的GUI表(水平不正常工作)

问题描述 投票:0回答:1

我正在使用python为我的任务创建一个表,但水平滚动条不能正常工作,垂直滚动条不在适当的位置。

我已经在python中创建了一个带有水平和垂直滚动条的n表。垂直滚动条工作正常但水平不滚动。该表仅显示5列和7行,其余列和行可以使用滚动查看。

我的问题是水平滚动条延伸到最后一列。比如,有10列,其中只有5列必须首先显示,但水平延伸到最后一列。而且,垂直滚动条不在适当的位置。

下面的代码就是一个例子。

# 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

滚动条必须严格位于指定的角落。

python-3.x tkinter
1个回答
1
投票

对于vsb:

  • 它必须粘在列的最右边,即使用sticky =“nse”;或者在“frame_canvas”中添加另一列
  • 如果上述方法无效,请为vsb使用“rowspan”选项

对于hsb:

  • 在canvas.xview之后删除括号
  • 另外,使用sticky =“swe”
© www.soinside.com 2019 - 2024. All rights reserved.