Python Forum
[Tkinter] Passing variable to function.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Passing variable to function.
#1
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!
Error:
TypeError: update() missing 1 required positional argument: 'slide_value'
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()
Reply
#2
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)
KDog and coder19 like this post
Reply
#3
Thank you once again Yoriz! Smile
Yoriz likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Passing information with a function Krisve94 3 885 Jun-30-2021, 07:51 PM
Last Post: deanhystad
  return a variable from a function snakes 3 1,304 Apr-09-2021, 06:47 PM
Last Post: snakes
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 1,876 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  Passing arguments into function, tkinter nanok66 3 2,248 Apr-18-2020, 11:53 PM
Last Post: nanok66
  Global Variable in Event Function? What am I doing wrong here? p_hobbs 1 1,418 Nov-13-2019, 02:50 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020