Hello friends! Let us continue developing our GUI of image editing software. You can check out the previous post by clicking here, where we have learned to create a window and add a few buttons to it. In this post, we will be adding sliders to our GUI which will be used to adjust the brightness and contrast of the image. You can check out the video below.
We will continue developing our previous code. For placing the sliders we will use another frame. For creating the frame use the code below.
Frame2 = tk.Frame(window, height=20)
Frame2.pack(anchor=tk.NW)
Frame2 will be holding the sliders of brightness and contrast adjustments. We have anchored the Frame2 to the northwest corner in our window. Let us now create the sliders for our window.
brightnessSlider = tk.Scale(Frame2, label="Brightness", from_=0, to=2, orient=tk.HORIZONTAL,
length=screen_width, resolution=0.1, command=brightness_callback)
brightnessSlider.pack(anchor=tk.N)
contrastSlider = tk.Scale(Frame2, label="Contrast", from_=0, to=255, orient=tk.HORIZONTAL,
length=screen_width, command=contrast_callback)
contrastSlider.pack(anchor=tk.N)
The meaning of all the parameters is as follows:
1. Frame2:- The frame on which you want to place the slider(use window name if you do not want to create a frame).
2. label:- Name of the slider.
3. from_:- The lowest value of the slider.
4. to:- The highest value of the slider.
5. orient:- Orientation of the slider(horizontal/vertical).
6. length:- length of the slider.
7. resolution:- The smallest incremental value of the scale(by default this is 1).
8. command:- The method that will be executed when the slider is moved.
We have anchored the sliders to the north side of Frame2. Now let us define the methods that will be executed when the slider is moved.
def brightness_callback(brightness_pos):
print(brightness_pos)
def contrast_callback(contrast_pos):
print(contrast_pos)
As you can see above, the methods take one argument which returns the current position of the slider. We will print this to know the current slider position.
Our code is now ready to run.
Happy coding...!!!
For full code click here.
Comments
Post a Comment