I need to add upload button so that I can upload the picture and display with this class. Everything working but when I am adding the upload button its giving me some error and my code is not wokring.
JavaScript
x
45
45
1
import tkinter as tk
2
from tkinter import *
3
from PIL import Image, ImageTk
4
from tkinter import filedialog
5
6
class Layout:
7
8
def __init__(self,master):
9
self.master = master
10
self.rootgeometry()
11
self.canvas = tk.Canvas(self.master)
12
self.canvas.pack()
13
self.background_image = Image.open(self.openfn())
14
self.image_copy = self.background_image.copy()
15
self.background = ImageTk.PhotoImage(self.background_image)
16
self.loadbackground()
17
18
19
20
def loadbackground(self):
21
self.label = tk.Label(self.canvas, image = self.background)
22
self.label.bind('<Configure>',self.resizeimage)
23
self.label.pack(fill='both', expand='yes')
24
25
def openfn(self):
26
filename = filedialog.askopenfilename(title='open')
27
return filename
28
29
30
31
def rootgeometry(self):
32
x=int(self.master.winfo_screenwidth()*0.7)
33
y=int(self.master.winfo_screenheight()*0.7)
34
z = str(x) +'x'+str(y)
35
self.master.geometry(z)
36
37
def resizeimage(self,event):
38
image = self.image_copy.resize((self.master.winfo_width(),self.master.winfo_height()))
39
self.image1 = ImageTk.PhotoImage(image)
40
self.label.config(image = self.image1)
41
42
root = tk.Tk()
43
a = Layout(root)
44
root.mainloop()
45
Advertisement
Answer
Create the Button
widget within the class constructor and bind it with self.loadbackground
. Also, you don’t need to recreate the Label
widget every time instead use label.configure(image=yourimage)
.
Here is the code:
JavaScript
1
55
55
1
import tkinter as tk
2
from tkinter import *
3
from PIL import Image, ImageTk
4
from tkinter import filedialog
5
6
class Layout:
7
8
def __init__(self,master):
9
self.master = master
10
self.rootgeometry()
11
12
self.button = Button(self.master, text='Upload', command=self.loadbackground)
13
self.button.pack()
14
15
self.canvas = tk.Canvas(self.master)
16
self.canvas.pack(fill=BOTH, expand=True)
17
18
self.background_image = None
19
self.image_copy = None
20
self.background = None
21
22
self.label = tk.Label(self.canvas)
23
self.label.pack(fill='both', expand=True)
24
25
26
def loadbackground(self):
27
28
self.background_image = Image.open(self.openfn())
29
self.image_copy = self.background_image.copy()
30
31
self.background = ImageTk.PhotoImage(self.background_image.resize((self.canvas.winfo_width(), self.canvas.winfo_height())))
32
self.label.configure(image=self.background)
33
self.label.bind('<Configure>',self.resizeimage)
34
35
def openfn(self):
36
filename = filedialog.askopenfilename(title='open')
37
return filename
38
39
40
def rootgeometry(self):
41
x=int(self.master.winfo_screenwidth()*0.7)
42
y=int(self.master.winfo_screenheight()*0.7)
43
z = str(x) +'x'+str(y)
44
self.master.geometry(z)
45
46
def resizeimage(self,event):
47
image = self.image_copy.resize((event.width, event.height))
48
self.image1 = ImageTk.PhotoImage(image)
49
self.label.config(image = self.image1)
50
51
52
root = tk.Tk()
53
a = Layout(root)
54
root.mainloop()
55