类中的Python Tkinter滚动条

时间:2016-07-19 22:36:28

标签: python canvas tkinter scrollbar frame

我的目标是创建一个带有垂直和水平滚动的GUI TKinter类。

我有一个图表,其尺寸大于GUI的窗口尺寸。我将图表放在画布中以启用滚动条功能,允许垂直和水平滚动。

但是,当我运行代码时,图形大小会自动适应GUI窗口的大小,从而扭曲图形比率,滚动条功能不起作用。

有谁知道这个解决方案?这是代码:

from tkinter import * 

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

from matplotlib.figure import Figure


class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        Frame_parent = Frame(self, bg = 'white', borderwidth = 0, relief = FLAT)
        Frame_parent.pack(side = TOP, padx = 1, pady = 1)

        Canvas_parent = Canvas(Frame_parent, scrollregion = (0, 0, 2000, 2000))
        vbar=Scrollbar(Frame_parent, orient = VERTICAL)
        vbar.pack(side = RIGHT, fill = Y)
        vbar.config(command = Canvas_parent.yview)
        hbar=Scrollbar(Frame_parent, orient=HORIZONTAL)
        hbar.pack(side=BOTTOM,fill=X)
        hbar.config(command=Canvas_parent.xview)

        Canvas_parent.config(xscrollcommand=hbar.set, yscrollcommand = vbar.set)
        Canvas_parent.pack(side = LEFT, expand = True, fill = BOTH)

        Frame_child = Frame(Canvas_parent, bg = 'white', borderwidth = 2, relief = FLAT)
        Frame_child.pack(side = TOP, padx = 10, pady = 1)


        f = Figure(figsize=(6, 5), dpi=100)
        a = f.add_subplot(111)
        t = arange(0.0, 3.0, 0.01)
        s = sin(2*pi*t)
        a.plot(t, s)


        canvas = FigureCanvasTkAgg(f, Frame_child)
        canvas.show()
        canvas.get_tk_widget().pack(side = BOTTOM,  fill = BOTH,  expand = False)

if __name__ == "__main__":
    app = App()
    app.geometry("400x400+51+51")
    app.title("Test")
    app.mainloop()

1 个答案:

答案 0 :(得分:2)

首先请注意的是,您有许多无用的容器小部件。所以在我下面的解决方案中,我将删除所有无用的代码;我指的是与Frame_parentFrame_childCanvas_parent相关的所有内容。请注意,在我的解决方案中,我可以让这些小部件到位,但原则上,我不编写我不使用的代码。

第二个重要提示是您要滚动canvas的图表,但您的代码正在尝试滚动Canvas_parent窗口小部件。这是你问题的核心。因此,您需要使用FigureCanvasTkAgg.get_tk_widget()并对其返回的对象应用config()方法。

这意味着您需要更改:

Canvas_parent.config(xscrollcommand=hbar.set, yscrollcommand = vbar.set)

要:

canvas.get_tk_widget().config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)

以下是解决方案:

from tkinter import * 

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

from matplotlib.figure import Figure

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.configure(width=400, height=400)


        f = Figure(figsize=(6, 5), dpi=100)
        a = f.add_subplot(111)
        t = arange(0.0, 3.0, 0.01)
        s = sin(2*pi*t)
        a.plot(t, s)

        canvas = FigureCanvasTkAgg(f, self)
        canvas.show()
        canvas.get_tk_widget().grid(row=0, column=0)

        vbar=Scrollbar(self, orient = VERTICAL)
        vbar.grid(row=0, column=1)       
        hbar=Scrollbar(self, orient=HORIZONTAL)
        hbar.grid(row=1, column=0)


        canvas.get_tk_widget().config(xscrollcommand=hbar.set, yscrollcommand = vbar.set)

        hbar.config(command=canvas.get_tk_widget().xview)
        vbar.config(command=canvas.get_tk_widget().yview)



if __name__ == "__main__":
    app = App()
    app.geometry("400x400+51+51")
    app.title("Test")
    app.mainloop()

以下是运行上述程序的屏幕截图:

enter image description here

请注意,如果您希望拉伸滚动条以使其在代码中看起来更长,则可以为vbar.grid(..., sticky=N+S, ...)添加hbar选项,为hbar.grid(..., sticky=W+E, ...)添加hbar。< / p>