Reputation: 102
I am trying to read the value from a tkinter slider and use it by some functions as following
from tkinter import *
from tkinter import filedialog, Label
import cv2
from PIL import Image, ImageTk
panelA = None
panelB = None
def av_blur(k):
global panelB
dst_img = cv2.imread('result_img.jpg',0)
dst_img = cv2.blur(dst_img,(5,5))
k = int(k)
dst_img = cv2.blur(dst_img, (k, k))
...
def av_blur(k):
global panelB
dst_img = cv2.imread('result_img.jpg',0)
dst_img = cv2.blur(dst_img,(5,5))
k = int(k)
dst_img = cv2.blur(dst_img, (k, k))
...
root = Tk()
opt_frame = Frame(root)
opt_frame.pack( side=LEFT ,expand=1, fill='both')
img_frame = Frame(root)
img_frame.pack( side=RIGHT ,expand=1, fill='both')
src_frame = Frame(img_frame,background="black")
src_frame.pack( side=TOP ,expand=1, fill='both')
dst_frame = Frame(img_frame,background="white")
dst_frame.pack( side=BOTTOM ,expand=1, fill='both')
#
# Defining Blur functions
#
blur_sel = IntVar()
br_frame = Frame(opt_frame,width=200, height=100)
blur_sel = IntVar()
blur_sel = 4
blur_rad1 = Radiobutton(br_frame, text='Average Blur', value=0,variable=blur_sel,command=av_blur)
blur_rad2 = Radiobutton(br_frame, text='Gaussian Blur', value=1,variable=blur_sel,command=ga_blur)
blur_rad3 = Radiobutton(br_frame, text='Median Blur', value=2,variable=blur_sel)
blur_rad1.pack(side="left", expand=True)
blur_rad2.pack(side="left", expand=True)
blur_rad3.pack(side="left", expand=True)
br_frame.pack(anchor='w',side="top", fill=None,expand=0)
blur_slide = Scale(opt_frame, from_=1, to=32, length=600,tickinterval=4,command=av_blur,orient=HORIZONTAL)
blur_slide.pack(anchor="w", padx=10, pady=10)
root.mainloop()
The problem whenever I try to reas the slider value named k
I keep getting the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
TypeError: av_blur() missing 1 required positional argument: 'k'
I tried this simple example and still getting the same thing:
def open_file():
...
def blurring():
print(scale.get())
root = Tk()
opt_frame = Frame(root)
opt_frame.pack( side=LEFT ,expand=1, fill='both')
img_frame = Frame(root)
img_frame.pack( side=RIGHT ,expand=1, fill='both')
src_frame = Frame(img_frame,background="black")
src_frame.pack( side=TOP ,expand=1, fill='both')
dst_frame = Frame(img_frame,background="white")
dst_frame.pack( side=BOTTOM ,expand=1, fill='both')
#
# Defining main window menu
#
menu = Menu(root)
new_item = Menu(menu)
new_item.add_command(label='Open File', command=open_file)
new_item.add_separator()
new_item.add_command(label='Save File')
new_item.add_separator()
new_item.add_command(label='Exit')
menu.add_cascade(label='File', menu=new_item)
root.config(menu=menu)
#
# Defining Blur functions
#
scale = Scale(opt_frame,orient='horizontal', from_=0, to=128, command=blurring)
scale.pack()
root.mainloop()
But it worked when I tried the example in here.
Upvotes: 1
Views: 356
Reputation: 15226
Simplifying your 2nd example you need to have an argument handled in your function that is trying to get the value of Scale.
Scale is already passing an argument so you dont need to write a lambda to manage this.
Simple do this:
import tkinter as tk
def blurring(value):
print(value)
root = tk.Tk()
scale = tk.Scale(root, orient='horizontal', from_=0, to=128, command=blurring)
scale.pack()
root.mainloop()
Results:
Upvotes: 1
Reputation: 1105
In the command
option for blur_slide
, change it either:
...command = lambda: av_blue(k) # where k is the value you pass to the function
EDIT
The lambda
function is used here to pass the argument k
when the slider is clicked or changes position.
If k
is a computed value, then the computation has to be done in a different function (placed under the command
option) as:
...command = precompute
def precomute():
# calculate k here
av_blur(k)
or change the function defintion to:
def av_blur():
# your code here
Upvotes: 0