Python Tkinter滚动条只是调整其所在框架的大小,而不是使画布可滚动

时间:2018-07-17 23:13:35

标签: python python-2.7 tkinter scrollbar tkinter-canvas

我想制作一个滚动条,以在装有照片的画布上滚动。我面临的问题是,框架没有变大,而没有使用滚动条。不管我做什么,Frame总是使用填充有图片的画布来调整大小。如果我将maxsize强制为任何值,则Frame不会变大,但是Scrollbar也不能滚动。 这是我写的代码:

from Tkinter import *
from tkFileDialog import *
from PIL import Image, ImageTk

class Gui(Frame):

    def __init__(self, master=None):
        Frame.__init__(self,master)
        self.pack(expand=False, fill=BOTH)
        self.master.title("Picscroll")
        self.master.minsize(400,400)


        scroll = Picscroll(self)
        scroll.pack(fill=Y, side=LEFT)


class Picscroll(Frame):

    def __init__(self, master=None):
        Frame.__init__(self,master)

        canvas = Canvas(self)
        canvas.config(scrollregion=canvas.bbox("all"))

        yscrollbar = Scrollbar(self, width=16, orient=VERTICAL)
        yscrollbar.pack(side=RIGHT, fill=Y)
        yscrollbar.config(command=canvas.yview)

        canvas.config(yscrollcommand=yscrollbar.set)
        canvas.pack(side=LEFT, expand=True, fill = BOTH)

        size = 64, 64               #rendering TestPic
        File = r"Download.png"
        img = Image.open(File)
        img.thumbnail(size)
        render = ImageTk.PhotoImage(img)

        for i in range(20):         #filling canvas with testpics
            for j in range(2):
                label1 = Label(canvas, image=render)
                label1.image = render
                label1.grid(column=j, row=i)


a = Gui()
a.mainloop()

请原谅我的英语能力

0 个答案:

没有答案