https://realpython.com/python-gui-tkinter/
Widżet 'Button' jest używany do wyświetlania klikalnego przycisku. Można go skonfigurować tak, aby naciśnięcie przycisku wywoływało pewną funkcję.
Funkcja uruchomiona przez naciśnięcie przycisku blokuje całą aplikację do chwili swojego zakończenia [command=lambda: time.sleep(10)]. Jeżeli działanie funkcji ma zajmować czas dłuższy niż jedna sekunda, wtedy należy rozważyć użycie mechanizmu wątków.
Wiele 'keyword arguments' z widżetu 'Label' działa także w widżecie 'Button'.
# button1.py
import tkinter as tk # Py3
root = tk.Tk()
def callback():
print("called the callback!")
#button = tk.Button(root, text="Click")
button = tk.Button(root,
text="Click",
width=25,
height=5,
bg="blue", # zmiana koloru w obecności wskaźnika
fg="yellow", # zmiana koloru w obecności wskaźnika
command=callback,
)
button.grid()
root.mainloop() # run the tkinter event loop
Wybrane 'keyword arguments' dla Button()
master=root # pierwszy argument
text="Click"
textvariable=strvar # a variable linked to the button
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 # funkcja wywoływana po kliknięciu przycisku
command=sys.exit # zakończenie programu
command=lambda: print("message in the command line")
command=root.quit # zakończenie działania pętli zdarzeń
command=top.destroy # zamknięcie okna top-level
# 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