画布上的滚动条

时间:2017-07-19 12:01:51

标签: python-3.x tkinter

我目前正在尝试在画布上实现滚动条,因为我了解到我无法立即在框架上执行此操作。我可以让它出现,但我实际上无法使它工作。在谈到python和tkinter时,我仍然是初学者,此前此帖中的帖子对我帮助不大。这是我的代码(我可以提供有关我已经做过的任何其他事情的建议,也被视为不良做法):

from tkinter import *

class myApp():
    def __init__(self,root):
        myApp.f2=Frame(root)
        myApp.f2.pack()
        myApp.canv=Canvas(self.f2)
        myApp.canv.pack()
        myApp.f1=Frame(self.canv)
        myApp.f1.pack(side=LEFT, fill=BOTH, expand=TRUE)
        myApp.scroll=Scrollbar(self.f1,orient=VERTICAL,
        command=myApp.canv.yview)
        myApp.scroll.grid(row=0,column=6)
        myApp.canv.config(yscrollcommand=myApp.scroll.set)

我必须在其他小部件中使用网格,我还没有包含在这里。

1 个答案:

答案 0 :(得分:0)

我真的不明白绑定是如何工作的,但这里是我在Toplevel中用于滚动条的代码,它不是来自我,但我不记得我在哪里找到它(我认为它是在stackoverflow上,你应该搜索更多,我相信你会找到一些东西)。它应该可以工作,但只有当你用鼠标悬停它时才能滚动条形

    Toplevel = tk.Toplevel(self)

    #create canvas to make a scrollbar
    canvas = tk.Canvas(Toplevel, borderwidth=0)
    #create frame which will contains your widgets
    frame = tk.Frame(canvas)

    #create and pack your vsb to the Toplevel and link it to the canvas yview
    vsb = tk.Scrollbar(Toplevel, orient="vertical", command=canvas.yview)
    canvas.configure(yscrollcommand=vsb.set)
    vsb.pack(side="right", fill="y")
    canvas.pack(side="left", fill="both", expand=True)
    canvas.create_window((5,5), window=frame, anchor="nw")


    #i don't understand this line
    frame.bind("<Configure>", lambda event, canvas=canvas: canvas.configure(scrollregion=canvas.bbox("all")))

    #add your widgets to the frame
    ...

PS:不要使用from tkinter import *您可以(并且将)发生名称冲突,使用import tkinterimport tkinter as tk

修改:this question是我的来源。

相关问题