import tkinter as tk from tkinter import ttk root = tk.Tk() # create a ttk Button widget button = ttk.Button(root, text="Click me!") # pack the button widget to display it in the window button.pack() root.mainloop()
import tkinter as tk from tkinter import ttk def say_hello(): print("Hello, world!") root = tk.Tk() # create a ttk Button widget with a command button = ttk.Button(root, text="Click me!", command=say_hello) # pack the button widget to display it in the window button.pack() root.mainloop()
import tkinter as tk from tkinter import ttk root = tk.Tk() # create a ttk Button widget with a different style style = ttk.Style() style.configure('Custom.TButton', foreground='red', font=('Arial', 18)) button = ttk.Button(root, text="Click me!", style='Custom.TButton') # pack the button widget to display it in the window button.pack() root.mainloop()This code creates a ttk Button that says "Click me!" with a custom style defined by the configure() method of the ttk.Style class. The style sets the foreground color to red and the font to Arial size 18. The style is applied to the button using the style='Custom.TButton' parameter in the ttk.Button widget. In conclusion, these code examples demonstrate the usage of the tkinter.ttk Button widget in Python for creating, customizing, and assigning a command to a button in a graphical user interface. The tkinter.ttk module is a library that is part of the standard Python GUI toolkit and provides access to common GUI widgets with a modern look and feel.