![]() |
[Tkinter] Passing variable to function. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Passing variable to function. (/thread-33765.html) |
Passing variable to function. - KDog - May-25-2021 Hi all. I have an app that displays live webcam feed. I have created a brightness slider that almost works. It changes the frame brightness but then the update function stops unless I move the slider. I am a beginner so very confused on this!
from tkinter import * import cv2 from PIL import Image, ImageTk, ImageEnhance import time from tkinter import filedialog import numpy as np class App: def __init__(self, video_source=0): self.overlay_img = None self.appName = "Kamera" self.window = Tk() self.window.title(self.appName) self.window.resizable(0, 0) # self.window.wm_iconbitmap("cam.ico") self.window['bg'] = 'black' self.video_source = video_source self.vid = MyVideoCapture(self.video_source) # self.label = Label(self.window, text=self.appName, font=15, bg='blue', fg='white').pack(side=TOP, fill=BOTH) self.canvas = Canvas(self.window, width=self.vid.width, height=self.vid.height, bg='red') self.canvas.pack() self.btn_snapshot = Button(self.window, text="Snapshot", width=5, command=self.snapshot) self.btn_snapshot.pack(side=LEFT, padx=10) self.btn_overlay = Button(self.window, text="Overlay", width=7, command=self.overlay) self.btn_overlay.pack(side=LEFT, padx=10) self.btn_settings = Button(self.window, text="Settings", width=5, command=self.settings) self.btn_settings.pack(side=LEFT, padx=10) self.slide_value = 0 self.update(self.slide_value) self.window.mainloop() def settings(self): self.newWindow = Toplevel(self.window) self.newWindow.title("Settings") self.newWindow.geometry("400x400") self.btn_flip = Button(self.newWindow, text="Mirror Image", width=10, command=self.flip_img) self.btn_flip.pack(side=LEFT, padx=10) # self.brightness_lbl = Label(self.newWindow, text="Image Brightness") # self.brightness_lbl.pack(anchor=NW) #var = IntVar() self.brightness = Scale( self.newWindow, length=200, from_=0, to=255, orient=HORIZONTAL, label="Image Brightness", command=self.slide) self.brightness.pack(anchor=NW) def slide(self, var): self.slide_value = self.brightness.get() print(self.slide_value) self.update(self.slide_value) def flip_img(self): self.vid.flipped = not self.vid.flipped def overlay(self): file = filedialog.askopenfile( mode='rb', defaultextension='.png',title="Choose Overlay Image", filetypes=[("PNG Files", '*.png')]) if file: self.overlay_img = ImageTk.PhotoImage(file=file) def snapshot(self): isTrue, frame = self.vid.getFrame() if isTrue: filename = filedialog.asksaveasfilename( defaultextension='.jpg', title="Choose Filename", filetypes=[("JPEG Files", '*.jpg')]) # image = "IMG-" + time.strftime("%H-%M-%S-%d-%m") + ".jpg" cv2.imwrite(filename, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) # msg = Label(self.window, text='Image Saved' + image, bg='black', fg='green').place(x=430, y=510) else: messagebox.showerror("paint says", "unable to save image ,\n something went wrong") if isTrue and self.overlay_img: filename = filedialog.asksaveasfilename( defaultextension='.jpg', title="Choose Filename", filetypes=[("JPEG Files", '*.jpg')]) filename = self.stamped_img def update(self, slide_value): isTrue, frame = self.vid.getFrame() frame = cv2.normalize(frame, frame, 0, slide_value, cv2.NORM_MINMAX) if isTrue: self.photo = ImageTk.PhotoImage(image=Image.fromarray(frame)) #self.canvas.tag_lower(self.photo) self.canvas.create_image(0, 0, image=self.photo, anchor=NW) if self.overlay_img: #self.canvas.tag_raise(self.overlay_img) self.canvas.create_image(0,0, image=self.overlay_img, anchor=NW) #self.stamped_img = self.photo.paste(self.overlay_img) self.window.after(100, self.update) class MyVideoCapture: def __init__(self, video_source=0): self.vid = cv2.VideoCapture(video_source) if not self.vid.isOpened(): raise ValueError("Unable to open this camera \n select another video source", video_source) self.width = self.vid.get(cv2.CAP_PROP_FRAME_WIDTH) self.height = self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT) self.flipped = True def getFrame(self): if self.vid.isOpened(): isTrue, frame = self.vid.read() if isTrue and self.flipped: frame = cv2.flip(frame, 1) if isTrue: return (isTrue, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) else: return (isTrue, None) else: return (isTrue, None) def __del__(self): if self.vid.isOpened(): self.vid.release() if __name__ == "__main__": App() RE: Passing variable to function. - Yoriz - May-25-2021 The error happens becasue self.window.after(100, self.update) calls the method def update(self, slide_value) with no slide_value.you don't need to call update it happens every 100 ms. remove self.update(self.slide_value) from the slide method the value has been set with self.slide_value = self.brightness.get() , that's all that's needed.def slide(self, var): self.slide_value = self.brightness.get() print(self.slide_value)then change update method to remove the slide_value parameter and change the passed in slide_value to self.slide_value def update(self): isTrue, frame = self.vid.getFrame() frame = cv2.normalize(frame, frame, 0, self.slide_value, cv2.NORM_MINMAX) RE: Passing variable to function. - KDog - May-25-2021 Thank you once again Yoriz! ![]() |