在Tkinter中从小创建大形象

时间:2019-04-19 15:02:53

标签: python-3.x

我在网上搜索了按照lightbox或fancybox的原理在网页中建立的图片库示例,并在python中创建。不幸的是,我没有找到任何东西。结果,我使这段代码运行良好,但是我必须为每个图像创建一个与其对应的Toplevel。这使得代码有点沉重。 这是python 3.6中的代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from tkinter import *

root=Tk()

def make_big_image1() :
    window_image=Toplevel(root)
    window_image.minsize(width=500, height=350)
    window_image.title("big image!")
    img=PhotoImage(file='images/big_images/big_image1.png')
    label = Label(window_image,image=img)
    label.image = img 
    label.pack()

def make_big_image2() :
    window_image=Toplevel(root)
    window_image.minsize(width=500, height=350)
    window_image.title("big image!")
    img=PhotoImage(file='images/big_images/big_image2.png')
    label = Label(window_image,image=img)
    label.image = img 
    label.pack()

def make_big_image3() :
    window_image=Toplevel(root)
    window_image.minsize(width=500, height=350)
    window_image.title("big image!")
    img=PhotoImage(file='images/big_images/big_image3.png')
    label = Label(window_image,image=img)
    label.image = img 
    label.pack()

small_img1=PhotoImage(file='images/small_images/small_image1.png')
small_img2=PhotoImage(file='images/small_images/small_image2.png')
small_img3=PhotoImage(file='images/small_images/small_image3.png')

bt1=Button(root, image=small_img1, command =make_big_image1)
bt1.pack()
bt2=Button(root, image=small_img2, command =make_big_image2)
bt2.pack()
bt3=Button(root, image=small_img3, command =make_big_image3)
bt3.pack()

lb=Label(root,text='click on image to make big image')
lb.pack()

root.mainloop()

我需要帮助来改进此代码或遵循另一种替代方法

0 个答案:

没有答案