Tkinter Button: Create, Customize, and Handle Clicks
In Tkinter, a button is created using the 'Button' class. Here's an example of creating a button in Tkinter:
import tkinter as tk
def button_clicked():
print('Button clicked')
root = tk.Tk()
button = tk.Button(root, text='Click me', command=button_clicked)
button.pack()
root.mainloop()
In this example, we create a button with the text 'Click me' using the 'Button' class. We also provide a 'command' parameter that specifies the function to be executed when the button is clicked (in this case, 'button_clicked'). The 'pack' method is used to add the button to the root window.
When the button is clicked, the 'button_clicked' function is called, and it simply prints 'Button clicked' to the console.
You can customize the appearance of the button by passing various parameters to the 'Button' constructor, such as 'font', 'bg' (background color), 'fg' (foreground color), 'width', 'height', etc.
原文地址: https://www.cveoy.top/t/topic/lNhg 著作权归作者所有。请勿转载和采集!