How can I set the default value of my Tkinter Scale widget slider to 100?

TkinterServer Side ProgrammingProgramming

Tkinter Scale Widgets are used to provide the visual representation of data items where we can modify or change the value of data items. In order to change the state of data or select multiple data in the field, we can use the Scale widget.

The Scale Widget can be created by initializing a Scale(parent, from_, to, orient, options) constructor.

However, sometimes we need to set the default value of the Tkinter Scale widget, which can be set by using the set(numeric_value) method.

Example

In this example, we will create a scale widget that sets a default value for the slider.

#Import the required Libraries
from tkinter import *
from tkinter import ttk
#Import the required Libraries
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame
win = Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
#Define a function to reset the slider
def reset_slider():
   horizontal.set(100)
#Initialize a Horizontal Scale Widget
horizontal=Scale(win, from_=0, to=200, orient= HORIZONTAL)
#Set the Default Value of Scale Widget
horizontal.set(100)
horizontal.pack()
#Create a button to reset the slider
ttk.Button(win, text= "Reset", command= reset_slider).pack(pady=15)
win.mainloop()

Output

Now, run the above code to display the Slider that has some default value.

In the given output, when we modify the slider value, we can reset it by clicking the "Reset" Button.

raja
Published on 03-May-2021 09:03:28
Advertisements