Tkinter Box的滚动配置

时间:2018-12-13 16:15:52

标签: python tkinter

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)}))
  1. 有没有一种方法可以“冻结”表的第一行,以便在向下滚动时列名不会消失?
  2. 如何将滚动敏感区域更改为整个表格,而不仅仅是条形图?目前,只有在鼠标悬停在栏区域上的情况下,栏才会响应滚动。

第二个问题与this有关,但后者未收到答案。

0 个答案:

没有答案