Reputation: 31
I'm trying to create a window to display a dataframe. However, the dataframe is way too big so I want to add scrollbars to be able to see the data. The problem is the scrollbars appear but don't work. I have tried several solutions I found here but none of these seem to work for me.
Here is my code:
import tkinter as tk
root = tk.Tk()
#create frame
frame = tk.Frame(root)
frame.grid(sticky = 'news')
frame.grid_propagate(False)
#create Canvas
canvas = tk.Canvas(frame, bg = 'white')
canvas.grid(row = 0, column = 0)
#scrollbars
yscrollbar = tk.Scrollbar(frame, orient = tk.VERTICAL, command = canvas.yview)
yscrollbar.grid(row = 0, column = 1, sticky = tk.NS)
canvas.configure(yscrollcommand=yscrollbar.set)
xscrollbar = tk.Scrollbar(frame, orient = tk.HORIZONTAL, command = canvas.xview)
xscrollbar.grid(row = 1, column = 0, sticky = tk.EW)
canvas.configure(xscrollcommand=xscrollbar.set)
#new frame to contain text
frame_text = tk.Frame(canvas)
canvas.create_window((0, 0), window=frame_text, anchor='nw')
#insert dataframe
values = [['Abbreviation', 'UNIPROT Code', 'Full Name', 'Sample Type', 'Sample', 'Patients AGE±SD', 'Condition', 'Recurrence', 'Surgery', 'Patients Comorbidities', 'Patients (n)', 'Controls AGE±SD'],['MYL4', 'P12829', 'Myosin light chain 4', 'tissue', 'Left atrial appendages', '68.2±2.9', 'permanent', 'n', 'mitral valve surgery', 'MR (11)', '11.0', '58,8 ± 4,3'], ['AHSG', 'P02765', 'alpha2-HS glycoprotein', 'tissue', 'Right atrial appendages', '72±10', 'permanent (5), persistent (2), paroxysmal (1)', 'n', 'CABG or valve surgery', 'Hypertension (6), CAD (4)', '8.0', '65±12']]
widgets = []
for row in range(len(values)):
current_row = []
for column in range(len(values[0])):
label = tk.Label(frame_text, text=values[row][column],borderwidth=0, width=len(str(values[row][column])))
label.grid(row=row, column=column, sticky="nsew", padx=1, pady=1)
current_row.append(label)
widgets.append(current_row)
for column in range(len(values[0])):
frame_text.grid_columnconfigure(column, weight=1)
#Resize the canvas frame
frame.config(width = 1000 + yscrollbar.winfo_width(),height=1000+
xscrollbar.winfo_width())
canvas.config(scrollregion=canvas.bbox("all"))
Also, how can I set the canvas size to be the same as the window? Because what I get now is: picture
Upvotes: 0
Views: 259
Reputation: 31
SOLVED
I realized the reason it wasn't working was because the scrollabe region was smaller than the canvas. So I changed:
frame.config(width = 1000 + yscrollbar.winfo_width(),height=1000 + xscrollbar.winfo_width())
canvas.config(scrollregion=canvas.bbox("all"))
to:
frame.config(width = root.winfo_width(),height=root.winfo_height())
canvas.config(width = root.winfo_width()-yscrollbar.winfo_reqwidth()-10,height=root.winfo_height()-xscrollbar.winfo_reqheight()-10,scrollregion=(0,0,frame_text.winfo_reqwidth(),frame_text.winfo_reqheight()))
Upvotes: 1
Reputation: 765
I've always assigned the command by configuring the scrollbars, not the canvas. Try replacing your lines:
canvas.configure...
With:
yscrollbar.config(command=canvas.yview)
Upvotes: 0