[파이썬] `tkinter` 스타일 및 테마

tkinter is a popular library in Python for creating graphical user interfaces(GUIs). It provides a set of widgets and functions that allow developers to design and build applications with a GUI. One of the key features of tkinter is the ability to customize the style and theme of the GUI elements to match the desired design aesthetic.

In this blog post, we will explore how to apply styles and themes to tkinter widgets to enhance the visual appeal of our applications.

Applying Styles to tkinter Widgets

tkinter provides two ways to apply styles to widgets - inline styling and named styles.

Inline Styling

Inline styling allows you to define the visual appearance of a widget directly when creating it. Here is an example of applying inline styles to a Button widget:

from tkinter import *

root = Tk()

button = Button(root, text="Click Me!", bg="red", fg="white", font=("Arial", 18))
button.pack()

root.mainloop()

In the above example, we have set the background color (bg), foreground color (fg), and font properties of the button to achieve the desired style.

Named Styles

Named styles provide a way to define a style once and apply it to multiple widgets. This is useful when you want consistent styling across your application. Here is an example of using named styles:

from tkinter import *

root = Tk()
style = Style()

style.configure("Red.TButton", background="red", foreground="white", font=("Arial", 18))

button1 = Button(root, text="Button 1", style="Red.TButton")
button1.pack()

button2 = Button(root, text="Button 2", style="Red.TButton")
button2.pack()

root.mainloop()

In the above example, we have defined a named style “Red.TButton” using the style.configure() method. We then apply this style to multiple Button widgets by setting the style parameter to the name of the style.

Applying Themes to tkinter Widgets

tkinter also provides the option to apply pre-defined themes to the widgets. Themes define a consistent set of styles for all the GUI elements. Here is an example of applying a theme to a tkinter application:

from tkinter import *
from tkinter.ttk import *

root = Tk()
style = Style()

style.theme_use('clam')

button = Button(root, text="Click Me!")
button.pack()

root.mainloop()

In the above example, we use the theme_use() method to apply the “clam” theme to our application. The “clam” theme provides a clean and modern look to the widgets.

tkinter provides several built-in themes such as “clam”, “default”, “alt”, “classic”, etc. You can experiment with different themes to find the one that best suits your application.

Conclusion

In this blog post, we have explored how to apply styles and themes to tkinter widgets to enhance the visual appeal of our applications. Whether it’s applying inline styles or using named styles and pre-defined themes, tkinter provides flexible options to customize the appearance of GUI elements. Experiment with different styles and themes to create stunning and intuitive user interfaces for your Python applications.