Tkinter Buttons

We need a button on our text entry form from the previous section. We're going to click the button and get a full name. To place a button on your form, add this line just before your grid lines:

btnFullName = tk.Button(form, text="Full Name")

We've set up a variable called btnFullName. On the right of an equals sign, we have tk then a dot. After the dot comes the keyword Button. In between the round brackets of Button, you need the name of the control that the button is going on (the form, for us). After a comma, you then need some text for your button. You can add things like width and height parameters, if you like. But we'll keep it simple. (However, there's something really important missing from the button code above, which we'll get to shortly.)

We can now add the button to the grid. Here's the line to add, just before the main loop line:

btnFullName.grid(columnspan=2, padx=15, pady=15)

Before we explain what the new columnspan parameter is, here's what your code should look like:

Python code for a Tkinter form with a Button

The grid line for the button doesn't have row and column parameters. It has a columnspan parameters. This is set to a value of 2. So this control will span two columns on our grid, column 0 and column 1. (There is a corresponding rowspan, if you wanted your control to span more than one row.)

You can run your program now, and see what it looks like:

Tkinter form with a button added

(If you want a bigger button, add a width parameter between the round brackets of Button on line 15 above.)

You may have noticed that if you click your button, nothing will happen. That's because we haven't wired it up yet.

To wire a button up, you need to add a parameter called command. The value for command is the name of a function. Change your Button line to this:

btnFullName = tk.Button(form, text="Full Name", command=setfullname)

After command= we have setfullname. This is just the name of a function. Because we haven't added the function yet, PyCharm will underline it with a message of "unresolved reference".

At the top of your code, just under import tkinter as tk, add the following function:

def setfullname():

print("Button clicked")

To test things out, we'll print to the output window, for the time being. Your code should look like this, though

Setting a command parameter for a Tkinter Button widget

Run your program. To see if it works, click your button. Now look at the output window. You should see the text "Button clicked" appear.

When the button is clicked, then, the function after the command parameter gets called. Whatever code you have in your function will get executed. Let's now get and set the contents for our Entry text boxes.

The top two Entry text boxes have the variable names firstEntry and surnameEntry in our code. You can use the method get after your text box name to get whatever text is in the text box. Like this:

f_name = firstEntry.get()
s_name = surnameEntry.get()

This then gets whatever is in your text boxes and places the contents into two new variables, f_name and s_name.

Setting text for an Entry text box is not quite as straightforward. One way is with the insert method:

fullNameEntry.insert(0, f_name + " " + s_name)

First, you need the name of the text box you're trying to insert text into. After a dot comes the insert method. In between round brackets, you need a position number. A number of 0 inserts the text at the first position, right at the start of your text box. After a comma, you need a string of text to place in the text box.

Another way to set text in an Entry text box is by setting up something called a StringVar. This is short for string variable. You set them up like this:

fullStringVar = tk.StringVar()

You need a variable to store your StringVar. This is then used between the round brackets of your Entry box:

fullNameEntry = tk.Entry(form, textvariable=fullStringVar)

Notice that the parameter name is textvariable.

What this does is to allow you to use your text variable to refer to any string that goes into or comes out of your text box:

fullStringVar.set( "Set some text here")

This time, the set method is used. In between the round brackets of set you only need the string of text you want in your text box. You don't need a position argument. Try them both out. First, change your setfullname function to this:

def setfullname():

f_name = firstEntry.get()
s_name = surnameEntry.get()
fullNameEntry.insert(0, f_name + " " + s_name)

Run your program. Enter a first name and surname in the top two text boxes. Press your button and you should see this:

Getting and setting text in a Tkinter Entry text box

Now try the other way of setting text. Set up a StringVar outside of your function:

form = tk.Tk()
form.title("My First Form")
fullStringVar = tk.StringVar()

For your fullNameEntry text box, change it to this:

fullNameEntry = tk.Entry(form, textvariable=fullStringVar)

Change your function to this:

def setfullname():

f_name = firstEntry.get()
s_name = surnameEntry.get()
fullStringVar.set(f_name + " " + s_name)

Run your program and try again. You should find that it works just as well.

What we'll do in the next lesson is to create a form with a dropdown menu at the top. The menu will have options to open a file, close a file, save a file, and quit the program. You'll then learn how to open a file with Python and read the contents into a text box.

Tkinter Menus >