[파이썬] tkinter 슬라이더(`Scale`) 위젯

Tkinter is a popular Python library for creating graphical user interfaces (GUI). One of the most commonly used widgets in tkinter is the Scale widget. The Scale widget allows users to select a value from a range by dragging a slider.

In this blog post, we will explore the basic functionality of the Scale widget and how to use it in a tkinter application.

Creating a tkinter window

Before we dive into the Scale widget, let’s first create a simple tkinter window:

import tkinter as tk

# Create a tkinter window
window = tk.Tk()

# Set the window title
window.title("Slider Demo")

# Set the window dimensions
window.geometry("400x200")

# Run the tkinter event loop
window.mainloop()

Creating a Scale widget

Now that we have our tkinter window set up, let’s add a Scale widget to it:

import tkinter as tk

# Create a tkinter window
window = tk.Tk()

# Set the window title
window.title("Slider Demo")

# Set the window dimensions
window.geometry("400x200")

# Create a Scale widget
slider = tk.Scale(window, from_=0, to=100, orient=tk.HORIZONTAL)

# Pack the Scale widget to the window
slider.pack()

# Run the tkinter event loop
window.mainloop()

In the above code, we create a Scale widget using the Scale class from the tkinter module. We set the range of values for the slider using the from_ and to parameters. We also set the orientation of the slider to be horizontal using the orient parameter. The Scale widget is then added to the window using the pack() method.

Getting the selected value

To retrieve the selected value from the Scale widget, we can use the get() method. Let’s update our code to display the selected value in a label:

import tkinter as tk

# Create a tkinter window
window = tk.Tk()

# Set the window title
window.title("Slider Demo")

# Set the window dimensions
window.geometry("400x200")

# Create a Scale widget
slider = tk.Scale(window, from_=0, to=100, orient=tk.HORIZONTAL)

# Pack the Scale widget to the window
slider.pack()

# Create a label to display the selected value
label = tk.Label(window, text="Selected Value: ")
label.pack()

# Function to update the label with the selected value
def update_label():
    value = slider.get()
    label.config(text=f"Selected Value: {value}")

# Bind the Scale widget to the update_label function
slider.bind("<ButtonRelease-1>", lambda event: update_label())

# Run the tkinter event loop
window.mainloop()

In the updated code, we create a label widget to display the selected value. We also define an update_label() function that retrieves the selected value using the get() method, and updates the label text. We bind the ButtonRelease-1 event of the Scale widget to the update_label() function, so that the label is updated whenever the user releases the mouse button after dragging the slider.

Conclusion

The Scale widget in tkinter is a versatile tool for implementing sliders in your Python GUI applications. It allows users to easily select a value from a range by dragging a slider. By using the get() method, you can retrieve the selected value and use it in your application logic.

I hope this blog post has given you a good introduction to the Scale widget in tkinter. Happy coding!