Dec-31-2022, 10:45 PM
I have a frame that stores a label which stores a background image. When I resize the form and the frame resizes, the background image of the label looks really choppy while it's being resized by the function. I was thinking about putting the function on a separate thread because I believe the image might look choppy because of it being on the main thread.
Here's what I've tried:
The error that I keep getting is:
Here's what I've tried:
from tkinter import Tk, Button, Label, Frame from tkinter import ttk, PhotoImage import threading import time from PIL import Image, ImageTk class MainWindow(Tk): def __init__(self): super().__init__() #create groupbox1 background image for modern look framing self.gb1_img = Image.open('imgs/groupbox-1.png') self.gb1_img = self.gb1_img.resize((260, 490), Image.Resampling.LANCZOS) self.gb1_img = ImageTk.PhotoImage(self.gb1_img) self.gb1_container = Frame(self, bg='#333333', width=280) self.gb1_container.pack(side='left', fill='y', padx=20, pady=(120, 20)) self.gb1_container.bind('<Configure>', self.start_resize_gb1) self.gb1 = Label(self.gb1_container, image=self.gb1_img, bg='#333333') self.gb1.place(x=0, y=0) def start_resize_gb1(self, e): t = threading.Thread(target=self.resize_gb1, args=(e,), daemon=1) t.start() #resize groupbox1 frame image def resize_gb1(self, e): self.gb1_img1 = Image.open('imgs/groupbox-1.png') self.gb1_img1 = self.gb1_img1.resize((e.width, e.height), Image.Resampling.LANCZOS) self.gb1_img1 = ImageTk.PhotoImage(self.gb1_img1) self.gb1.configure(image=self.gb1_img1) if __name__ == '__main__': app = MainWindow() app.title('Main Window') app.configure(bg='#333333') #center the Main Window: w = 1000 # Width h = 620 # Height app.geometry('%dx%d+%d+%d' % (w, h, x, y)) app.mainloop()Is there a way that I can bind the function on it's own thread so that it doesn't look so choppy while resizing?
The error that I keep getting is:
Exception in thread Thread-57 (resize_gb1): Traceback (most recent call last): File "C:\Users\Aaron\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner self.run() File "C:\Users\Aaron\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run self._target(*self._args, **self._kwargs) File "C:\Users\Aaron\Documents\Python\Project1\testing\MAIN.py", line 92, in resize_gb1 self.gb1_img1 = ImageTk.PhotoImage(self.gb1_img1) File "C:\Users\Aaron\AppData\Roaming\Python\Python310\site-packages\PIL\ImageTk.py", line 143, in __init__ self.paste(image) File "C:\Users\Aaron\AppData\Roaming\Python\Python310\site-packages\PIL\ImageTk.py", line 195, in paste im.load() File "C:\Users\Aaron\AppData\Roaming\Python\Python310\site-packages\PIL\ImageFile.py", line 268, in load self.load_end() File "C:\Users\Aaron\AppData\Roaming\Python\Python310\site-packages\PIL\PngImagePlugin.py", line 962, in load_end self.fp.read(4) # CRC AttributeError: 'NoneType' object has no attribute 'read'