https://realpython.com/python-gui-tkinter/
'Button' widgets are used to display 'clickable buttons'. They can be configured to call a function whenever they’re clicked.
Many keyword arguments from the 'Label' widget will work for the 'Button' widget.
# button1.py import tkinter as tk # Py3 root = tk.Tk() def callback(): print("called the callback!") #button = tk.Button(root, text="Click", command=callback) button = tk.Button(root, text="Click", width=25, height=5, bg="blue", # the color is changing with the presence of a pointer fg="yellow", command=callback, ) button.grid() root.mainloop() # run the tkinter event loop
Selected keyword arguments in Button() master=root # the first argument text="Click" textvariable=strvar # a variable linked to the button image=logo fg="white" # set the text color to white ('fg' or 'foreground') bg="black" # set the background color to black ('bg' or 'background') width=50 # in text units height=10 # in text units font="Times 10 bold" command=callback # an action to call when the widget's event occurs command=sys.exit # it shuts down the calling program command=lambda: print("message in the command line") command=root.quit # it ends the current mainloop event loop call command=top.destroy # it kills the current top-level window
# button2.py import tkinter as tk root = tk.Tk() label = tk.Label(root, text="Press any button") button1 = tk.Button(root, text="Hello", command=lambda: print("Hello!")) button2 = tk.Button(root, text="Quit", command=root.quit) label.grid(row=0, column=0, columnspan=2) button1.grid(row=1, column=0) button2.grid(row=1, column=1) root.mainloop() # run the tkinter event loop