Introducing a Few More Widgets

To end this chapter, we will look at a few more commonly used widgets.

Using Text

The Entry widget that we have been using since the start of this chapter allows for only a single line of text. If we want multiple lines of text, we use the Text widget instead, as shown here:

 import​ tkinter
 
 def​ cross(text):
  text.insert(tkinter.INSERT, ​'X'​)
 
 window = tkinter.Tk()
 frame = tkinter.Frame(window)
 frame.pack()
 
 text = tkinter.Text(frame, height=3, width=10)
 text.pack()
 
 button = tkinter.Button(frame, text=​'Add'​, command=​lambda​: cross(text))
 button.pack()
 
 window.mainloop()

Here is the resulting GUI:

Text provides a much richer set of methods than the other widgets we have seen so far. ...

Get Practical Programming, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.