Tkinter Basics #2 (Buttons & Entry Boxes)

Download:

Buttons:

Creating a button is relatively simple.

However, in order to make a button responsive we require a procedure that is used within the parameter when creating a button.

The procedure that we are going to make is going to output a label when the button is clicked.

Notice how it is placed on row 1. The button is going to be placed above the label on row 0.

Now we can make the button.

The button is placed as a grid on GUI_Window with the text output ‘Click me’. When clicked, it will execute the command / procedure ‘myClick()’.

The Result:

When Clicked:

There are several other parameters that can be added to Button().

  • ‘state = disabled’ | This greys out the button so it cannot be used by the user
  • ‘padx’ / ‘pady’ = number | This changes the size of the button based on it x & y axis value.
  • ‘fg’ (foreground) / ‘bg’ (background) = colour | This changes the colour of the button.

Entry Boxes:

The entry box that we are going to create is going to take an input, which will be the user’s name. We will then use the button as a ‘enter’ key. The program will then output ‘hello’ to the user.

We first define the Entry box:

We then place the entry box onto the grid in the GUI_Window:

As a bonus, we can insert some text in the input box. This is not going to be entered as the input for the entry, but more than an output message to the user.

We then have to change the text variable in the procedure: ‘myClick’:

.get() takes the data from the entry box, which then it assigned to a any variable. In this case, text.

The Result:

When Clicked:

 

 229 total views,  1 views today