12.07.2015 Views

Tkinter reference: A GUI for Python

Tkinter reference: A GUI for Python

Tkinter reference: A GUI for Python

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

15.2 Connecting scrollbars to other widgetsHere is a code fragment showing the creation of a canvas with horizontal and verticalscrollbars. In this fragment, self is assumed to be a Frame widget.Notes:self.canv = Canvas ( self, width=600, height=400,scrollregion=(0, 0, 1200, 800) )self.canv.grid ( row=0, column=0 )self.scrollY = Scrollbar ( self, orient=VERTICAL,command=self.canv.yview )self.scrollY.grid ( row=0, column=1, sticky=N+S )self.scrollX = Scrollbar ( self, orient=HORIZONTAL,command=self.canv.xview )self.scrollX.grid ( row=1, column=0, sticky=E+W )self.canv["xscrollcommand"] = self.scrollX.setself.canv["yscrollcommand"] = self.scrollY.set The connection goes both ways. The canvas’s xscrollcommand option has to beconnected to the horizontal scrollbar’s .set method, and the scrollbar’s commandoption has to be connected to the canvas’s .xview method. The vertical scrollbarand canvas must have the same mutual connection. The sticky options on the .grid() method calls <strong>for</strong> the scrollbars <strong>for</strong>ce them tostretch just enough to fit the corresponding dimension of the canvas.16. The Text widgetText widgets are a much more generalized method <strong>for</strong> handling multiple lines of textthan the Label widget. Text widgets are pretty much a complete text editor in a window: You can mix text in different fonts, colors, and backgrounds. You can intersperse embedded images with text. An image is treated as a singlecharacter. You can even embed in it a “window” containing any <strong>Tkinter</strong> widget—even a framewidget containing other widgets. A window is also treated as a single character. A text widget may contain invisible mark objects between character positions.To use a text widget, there are two other concepts you will need to understand: An index is a way of describing a specific position between two characters of a textwidget. Text widgets allow you to define names <strong>for</strong> regions of the text called tags. Youcan change the appearance of a tagged region, changing its font, <strong>for</strong>eground andbackground colors, and other attributes.Refer to the separate sections below <strong>for</strong> more on the theory and practice of indices, marks,images, windows, and tags.New Mexico Tech Computer Center <strong>Tkinter</strong> <strong>reference</strong>: The Text widget Page 49

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!