为什么在tkinter笔记本选项卡的tkinter框架(带画布)上使用tkinter滚动条,只有当鼠标光标滑过滚动滑块时才滚动?

时间:2017-11-04 09:54:19

标签: python tkinter scroll

这是短笔记本代码,仅当鼠标光标在滚动滑块上时才滚动。 找不到有关我的问题的任何信息。我在互联网等中发现的例子有时很好,但在我的情况下,不是。 Python3.4 win7 64.

# -*- coding: utf-8 -*-
import tkinter
from tkinter import ttk
import sys,pickle,webbrowser


def cocoa_click(event):
    webbrowser.open("http://stts.pythonanywhere.com/")

mainframe = tkinter.Tk()

mainframe_width = (mainframe.winfo_screenwidth() / 2 - 200)
mainframe_heigth = (mainframe.winfo_screenheight() / 2 - 250)
mainframe.geometry("%dx%d+%d+%d" % (1000, 600, mainframe_width, mainframe_heigth))
mainframe.title('DAS progress 132')
mainframe.resizable(False,True)
n_book = ttk.Notebook(mainframe)
n_book.pack(fill='both', expand='yes')
n_book.pressed_index = None

x_name=["tabnote1","tabnote2","tabnote3","tabnote4"]
x_container=[]; x_canvas=[]; x_scroll=[]; x_frame=[]
cvsw,cvsh=960,400;
sreg=(0,0,1000,1320)

for i in range(4):
    x_container.append(tkinter.Frame(n_book))
    x_container[i].pack(fill=tkinter.BOTH, expand=True)
    n_book.add(x_container[i], text=x_name[i]) # add container to note book
    x_canvas.append(tkinter.Canvas(x_container[i], width=cvsw, height=cvsh)) # add canvas to container
    x_scroll.append(tkinter.Scrollbar(x_container[i], command=x_canvas[i].yview)) # add scroll to container
    x_canvas[i].config(yscrollcommand=x_scroll[i].set, scrollregion=sreg)
    x_canvas[i].pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)
    x_scroll[i].config(width=240)
    x_scroll[i].pack(side=tkinter.RIGHT, fill=tkinter.Y, expand=True)


cocoa = ttk.Button(mainframe,text="(: cocoa") # add button to mainframe - worked
cocoa.place(height=40,width=600,relx=0.4,rely=0)
cocoa.bind("<Button-1>",cocoa_click)
mainframe.mainloop()

1 个答案:

答案 0 :(得分:0)

我找到了我的案例的脏解决方案,但它没有跨平台(linux有不同的行为)

# -*- coding: utf-8 -*-
import tkinter
from tkinter import ttk
import sys,pickle,webbrowser

def on_mousewheel(event):
    try:
        print("e ",event)
        shift = (event.state & 0x1) != 0
        canvas=event.widget
        scroll = -1 if event.delta > 0 else 1
        if shift:
            canvas.xview_scroll(scroll, "units")
        else:
            canvas.yview_scroll(scroll, "units")
    except:pass

def cocoa_click(event):
    webbrowser.open("http://stts.pythonanywhere.com/")

mainframe = tkinter.Tk()
mainframe_width = (mainframe.winfo_screenwidth() / 2 - 200)
mainframe_heigth = (mainframe.winfo_screenheight() / 2 - 250)
mainframe.geometry("%dx%d+%d+%d" % (1000, 600, mainframe_width, mainframe_heigth))
mainframe.title('DAS progress 132')
mainframe.resizable(False,True)
n_book = ttk.Notebook(mainframe)
n_book.pack(fill='both', expand='yes')
n_book.pressed_index = None

x_name=["tabnote1","tabnote2","tabnote3","tabnote4"]
x_container=[]; x_canvas=[]; x_scroll=[]; x_frame=[]
cvsw,cvsh=960,400;
sreg=(0,0,1000,1320)

for i in range(4):
    x_container.append(tkinter.Frame(n_book))
    x_container[i].pack(fill=tkinter.BOTH, expand=True)
    n_book.add(x_container[i], text=x_name[i]) # add container to note book
    x_canvas.append(tkinter.Canvas(x_container[i], width=cvsw, height=cvsh)) # add canvas to container
    x_scroll.append(tkinter.Scrollbar(x_container[i], command=x_canvas[i].yview)) # add scroll to container
    x_canvas[i].config(yscrollcommand=x_scroll[i].set, scrollregion=sreg)
    x_canvas[i].pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)
    x_scroll[i].config(width=240)
    x_scroll[i].pack(side=tkinter.RIGHT, fill=tkinter.Y, expand=True)
    x_canvas[i].bind_all("<MouseWheel>", on_mousewheel) # index always 3


cocoa = ttk.Button(mainframe,text="(: cocoa") # add button to mainframe - worked
cocoa.place(height=40,width=600,relx=0.4,rely=0)
cocoa.bind("<Button-1>",cocoa_click)
mainframe.mainloop()