from tkinter import Tk, Label, Scrollbar,\
Frame, Canvas
import pandas as pd
def testTable(df):
def onFrameConfigure(canvas):
canvas.configure(scrollregion=canvas.bbox("all"))
def mouse_wheel(event):
global mouseCount
if event.num == 5 or event.delta == -120:
mouseCount -= 1
if event.num == 4 or event.delta == 120:
mouseCount += 1
label['text'] = mouseCount
mouseCount = 0
rows= df.shape[0]
cols = df.shape[1]
root = Tk()
root.geometry("100x300")
canvas = Canvas(root)
frame = Frame(canvas)
scrollbar = Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((rows,cols), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
frame.bind("<MouseWheel>", mouse_wheel)
for i in range(rows):
for j in range(cols):
if i != 0:
Label(frame, text=df.loc[i,df.columns[j]], width=5, borderwidth="1",
relief="solid").grid(row=i+1, column=j)
else:
Label(frame, text=df.columns[j], width=5, borderwidth="1",
relief="solid").grid(row=i, column=j)
root.focus_force()
root.mainloop()
testTable(pd.DataFrame({'colA': range(20), 'colB': range(20)}))
第二个问题与this有关,但后者未收到答案。