Tkinter Frame and Scale integrated Example
We can use the Frame frame as a container and then place the three color scale bars inside this frame.
Implementation
Place the three color scale bars inside the Frame container, and then place the Frame container in the center above the window.
from tkinter import *
def bgUpdade(source): # Note:The parameter source is required here
''' Change window background color '''
red = rSlider.get() # Read red value
green = gSlider.get() # Read green value
blue = bSlider.get() # Read blue value
print("R=%d, G=%d, B=%d" % (red, green, blue)) # Print color values
myColor = "#%02x%02x%02x" % (red,green,blue) # Converting colors to hexadecimal strings
print(myColor) # Print this string representing the color
root.config(bg=myColor) # Set window background color
def resetColor():
rSlider.set(0)
gSlider.set(0)
bSlider.set(0)
root = Tk()
root.title("apidemos.com")
root.geometry("360x240")
fm = Frame(root) # Create Frame
fm.pack() # Automatically placed in the upper center
rSlider = Scale(fm,from_=0,to=255,label="Red Portion",command=bgUpdade)
gSlider = Scale(fm,from_=0,to=255,label="Green Portion",command=bgUpdade)
bSlider = Scale(fm,from_=0,to=255,label="Blue Portion",command=bgUpdade)
gSlider.set(125) # Set the initial value of the green component to 125
rSlider.grid(row=0, column=0) # row=0,col=0
gSlider.grid(row=0, column=1) # row=0,col=1
bSlider.grid(row=0, column=2) # row=0,col=2
btn = Button(root,text="Reset Color",command=resetColor)
btn.pack(pady=5)
root.mainloop()
Output:
The above program creates the frame fm object on lines 21 and 22, and then places the color scale bar inside this frame fm object on lines 24 to 26.