Here we demonstrate the concept of focus, which is easier to show than describe. When there are a group of widgets inside a window, only one widget can react to an event like the click of the mouse button. In this example, the button underneath the mouse cursor has focus and therefore is the one that will respond to a click of the mouse. As the cursor moves over another button, then that button has focus. In this example, the button that has focus changes its color, on a Linux-operating system. On MS Windows, the buttons do not change color but the mouse cursor changes.
Execute the program shown in the usual way.
#button_focus_1.py #>>>>>>>>>>>>>>> from Tkinter import * root = Tk() butn_widget_1 = Button(text='First, RAISED', padx=10, pady=10) butn_widget_1.config(cursor='gumby') butn_widget_1.config(bd=8, relief=RAISED) butn_widget_1.config(bg='dark green', fg='white') butn_widget_1.config(font=('helvetica', 20, 'underline italic')) butn_widget_1.grid(row=1, column = 1) butn_widget_2 = Button(text='Second, FLAT', padx=10, pady=10) butn_widget_2.config(cursor='circle') butn_widget_2.config(bd=8, relief=FLAT) butn_widget_2.grid(row=1, column = 2) butn_widget_3 = Button(text='Third, SUNKEN', padx=10, pady=10) butn_widget_3.config(cursor='heart') butn_widget_3.config(bd=8, relief=SUNKEN) butn_widget_3.config(bg='dark blue', fg='white') butn_widget_3.config(font=('helvetica', 30, 'underline italic')) butn_widget_3.grid(row=1, column = 3) butn_widget_4 = Button(text='Fourth, GROOVE', padx=10, pady=10) butn_widget_4.config(cursor='spider') butn_widget_4.config(bd=8, relief=GROOVE) butn_widget_4.config(bg='red', fg='white') butn_widget_4.config(font=('helvetica', 20, 'bold')) butn_widget_4.grid(row=1, column = 4) butn_widget_5 = Button(text='Fifth RIDGE', padx=10, pady=10) butn_widget_5.config(cursor='pencil') butn_widget_5.config(bd=8, relief=RIDGE) butn_widget_5.config(bg='purple', fg='white') butn_widget_5.grid(row=1, column = 5) root.mainloop( )
When we run the preceding code under Linux, we will see that the color of each button change as it acquires focus. The button that has focus is the only one of the group that will react to a left mouse click. Under MS Windows 7, this change of color with focus does not work. Nevertheless, the logic of focus behavior and reaction to mouse events is unaffected.
We have also taken the opportunity to look at the different button border styles available.
13.58.209.201