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:

images/gui/text.png

Text provides a much richer set of methods than the other widgets we have seen so far. We can embed images in the text area, put in tags, select particular lines, and so on. The exercises will give you a chance to explore its capabilities.

Using Checkbuttons

Checkbuttons, often called checkboxes, have two states: on and off. When a user clicks a checkbutton, the state changes. We use a tkinter mutable variable to keep track of the user’s selection. Typically, an IntVar variable is used, and the values 1 and 0 indicate on and off, respectively. In the following code, we use three checkbuttons to create a simpler color picker, and we use method config to change the configuration of a widget after it has been created:

 import​ tkinter
 
 window = tkinter.Tk()
 frame = tkinter.Frame(window)
 frame.pack()
 red = tkinter.IntVar()
 green = tkinter.IntVar()
 blue = tkinter.IntVar()
 
 for​ (name, var) ​in​ ((​'R'​, red), (​'G'​, green), (​'B'​, blue)):
  check = tkinter.Checkbutton(frame, text=name, variable=var)
  check.pack(side=​'left'​)
 
 def​ recolor(widget, r, g, b):
  color = ​'#'
 for​ var ​in​ (r, g, b):
  color += ​'FF'​ ​if​ var.get() ​else​ ​'00'
  widget.config(bg=color)
 
 label = tkinter.Label(frame, text=​'[ ]'​)
 button = tkinter.Button(frame, text=​'update'​,
  command=​lambda​: recolor(label, red, green, blue))
 button.pack(side=​'left'​)
 label.pack(side=​'left'​)
 window.mainloop()

Here is the resulting GUI:

images/gui/checkbutton.png

Using Menu

The last widget we will look at is Menu. The following code uses this to create a simple text editor:

 import​ tkinter
 import​ tkinter.filedialog ​as​ dialog
 
 def​ save(root, text):
  data = text.get(​'0.0'​, tkinter.END)
  filename = dialog.asksaveasfilename(
  parent=root,
  filetypes=[(​'Text'​, ​'*.txt'​)],
  title=​'Save as...'​)
  writer = open(filename, ​'w'​)
  writer.write(data)
  writer.close()
 
 def​ quit(root):
  root.destroy()
 
 window = tkinter.Tk()
 text = tkinter.Text(window)
 text.pack()
 
 menubar = tkinter.Menu(window)
 filemenu = tkinter.Menu(menubar)
 filemenu.add_command(label=​'Save'​, command=​lambda​ : save(window, text))
 filemenu.add_command(label=​'Quit'​, command=​lambda​ : quit(window))
 
 menubar.add_cascade(label = ​'File'​, menu=filemenu)
 window.config(menu=menubar)
 
 window.mainloop()

The program begins by defining two functions: save, which saves the contents of a text widget, and quit, which closes the application. Function save uses tkFileDialog to create a standard “Save as...” dialog box, which will prompt the user for the name of a text file.

After creating and packing the Text widget, the program creates a menu bar, which is the horizontal bar into which we can put one or more menus. It then creates a File menu and adds two menu items to it called Save and Quit. We then add the File menu to the menu bar and run mainloop.

Here is the resulting GUI:

images/gui/menu.png
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.197.95