繁体   English   中英

如何使用上传按钮显示图像并使用 window 调整大小(动态)使用 Tkinter python 调整大小,我需要添加上传按钮

[英]How to display image with upload button in and resize it with window resize (Dynamically)with Tkinter python, I need to add upload button

我需要添加上传按钮,以便我可以使用此 class 上传图片并显示。 一切正常,但是当我添加上传按钮时,它给了我一些错误,并且我的代码无法正常工作。

import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog

class Layout:
    
    def __init__(self,master):
        self.master = master
        self.rootgeometry()
        self.canvas = tk.Canvas(self.master)
        self.canvas.pack()
        self.background_image = Image.open(self.openfn())
        self.image_copy = self.background_image.copy()
        self.background = ImageTk.PhotoImage(self.background_image)
        self.loadbackground()
             
                   

    def loadbackground(self):
        self.label = tk.Label(self.canvas, image = self.background)
        self.label.bind('<Configure>',self.resizeimage)
        self.label.pack(fill='both', expand='yes')
    
    def openfn(self):
        filename = filedialog.askopenfilename(title='open')
        return filename
    


    def rootgeometry(self):
        x=int(self.master.winfo_screenwidth()*0.7)
        y=int(self.master.winfo_screenheight()*0.7)
        z = str(x) +'x'+str(y)
        self.master.geometry(z)

    def resizeimage(self,event):
        image = self.image_copy.resize((self.master.winfo_width(),self.master.winfo_height()))
        self.image1 = ImageTk.PhotoImage(image)
        self.label.config(image = self.image1)

root = tk.Tk()
a = Layout(root)
root.mainloop()

在 class 构造函数中创建Button小部件并将其与self.loadbackground绑定。 此外,您不需要每次都重新创建Label小部件,而是使用label.configure(image=yourimage)

这是代码:

import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog

class Layout:
    
    def __init__(self,master):
        self.master = master
        self.rootgeometry()

        self.button = Button(self.master, text='Upload', command=self.loadbackground)
        self.button.pack()
        
        self.canvas = tk.Canvas(self.master)
        self.canvas.pack(fill=BOTH, expand=True)

        self.background_image = None
        self.image_copy = None
        self.background = None

        self.label = tk.Label(self.canvas)
        self.label.pack(fill='both', expand=True)
        

    def loadbackground(self):

        self.background_image = Image.open(self.openfn())
        self.image_copy = self.background_image.copy()
        
        self.background = ImageTk.PhotoImage(self.background_image.resize((self.canvas.winfo_width(), self.canvas.winfo_height())))
        self.label.configure(image=self.background)
        self.label.bind('<Configure>',self.resizeimage)

    def openfn(self):
        filename = filedialog.askopenfilename(title='open')
        return filename
    

    def rootgeometry(self):
        x=int(self.master.winfo_screenwidth()*0.7)
        y=int(self.master.winfo_screenheight()*0.7)
        z = str(x) +'x'+str(y)
        self.master.geometry(z)

    def resizeimage(self,event):
        image = self.image_copy.resize((event.width, event.height))
        self.image1 = ImageTk.PhotoImage(image)
        self.label.config(image = self.image1)
        

root = tk.Tk()
a = Layout(root)
root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM