当x截屏的总面积超过窗口的总面积时,在tkinter中启用滚动条

时间:2018-03-27 05:17:52

标签: python tkinter tk

我正在开发一个tkinter个人项目,该项目涉及显示可滚动的图像库。所有图像都具有相同的分辨率(1920x1080)。图像不断重新调整以适应不断变化的框架/窗口尺寸。如果所有图像都不能在一个视图中显示,我想添加一个垂直滚动条来查看不适合一个视图的图像。出于测试目的,我有一个12个图像对象的列表,我迭代并添加到框架。为了使框架可滚动,I've taken the advice of others和我在画布中嵌入了框架,使画布小部件可滚动。但是,如果我注释掉我的评论中注释掉的那一行,并取消注释我的评论说取消注释的行,则滚动条不能正常运行,但图像会重新洗牌。另一方面,如果我只是按原样保留所有内容,则图像不会被重新调整,但滚动条可以正常工作。需要做些什么才能使滚动功能和重组功能一次性运行?

from tkinter import *
from PIL import Image, ImageTk
image_objs  = [Image.open('testimage1.png'),Image.open('testimage2.png'),Image.open('testimage3.png'),
           Image.open('testimage4.png'),Image.open('testimage5.png'),Image.open('testimage6.png'),
           Image.open('testimage7.png'),Image.open('testimage8.png'),Image.open('testimage9.png'),
           Image.open('testimage10.png'),Image.open('testimage11.png'),Image.open('testimage12.png')]
root = Tk()
canvas = Canvas(root, borderwidth =0, bg = 'Gold')

f = Frame(canvas,  height = 500,width = 500, bg = 'Gold')
widget_list = list()
#f.pack(fill = BOTH, expand = YES) #uncomment this
vsb = Scrollbar(root, orient= 'vertical', command = canvas.yview)
canvas.configure(yscrollcommand = vsb.set)
vsb.pack(side = "right", fill = "y")
canvas.pack(fill = BOTH, expand = YES)
canvas.create_window((0,0), window = f, anchor = 'nw') #comment this out

def configure(event):
    global widget_list
    canvas.configure(scrollregion=canvas.bbox("all"))
    horizontal_ammount = f.winfo_width()//(1920//6) #Image w[![enter image description here][1]][1]idth/ window     width
    image_obj_iter = iter(image_objs)
    for x in widget_list:
        x.grid_forget()
    widget_list = list()
    try:
        if horizontal_ammount > 0:
            for x in range(12//horizontal_ammount):
                for y in range(horizontal_ammount):
                    img = next(image_obj_iter)
                    img = img.resize((img.size[0]//6, img.size[1]//6))
                    img = ImageTk.PhotoImage(img)
                    label = Label(master= f,image = img)
                    label.image = img
                    widget_list.append(label)
                    label.grid(row = x, column = y)
    except StopIteration:
        return

f.bind("<Configure>", configure)
root.mainloop()

-----没有工作滚动条的重新洗牌版------- enter image description here

-----带有工作滚动条的非重新洗牌版----- enter image description here

0 个答案:

没有答案