9

I was wondering if this is even possible. My goal is to have a small white box in the bottom right hand corner, on top of a larger text field. The white box will be used as an "info box" while the person scrolls through the text inside of the text field.

When I say "text field" I mean Text from tkinter.

user3033423
  • 151
  • 1
  • 2
  • 7

1 Answers1

9

The way to place one widget on top of other widgets is to use the place geometry manager. You can specify an x/y coordinate relative to some other widget, as well as to specify either absolute or relative widths and heights.

The effbot site has a decent writeup on the place geometry manager: http://effbot.org/tkinterbook/place.htm

Here's a simple example:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.text = tk.Text(self, wrap="word")
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.text.yview)
        self.text.configure(yscrollcommand=self.text_yview)
        self.vsb.pack(side="right", fill="y")
        self.text.pack(side="left", fill="both", expand=True)

        # create an info window in the bottom right corner and
        # inset a couple of pixels
        self.info = tk.Label(self.text, width=20, borderwidth=1, relief="solid")
        self.info.place(relx=1.0, rely=1.0, x=-2, y=-2,anchor="se")

    def text_yview(self, *args):
        ''' 
        This gets called whenever the yview changes.  For this example
        we'll update the label to show the line number of the first
        visible row. 
        '''
        # first, update the scrollbar to reflect the state of the widget
        self.vsb.set(*args)

        # get index of first visible line, and put that in the label
        index = self.text.index("@0,0")
        self.info.configure(text=index)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thank you, it worked perfectly. I have another question, however. I am trying to find out how to find the current position of the text cursor in a Text TKinter python project. Text cursor meaning the "|" that blinks next to where your writing. – user3033423 Nov 27 '13 at 03:41
  • @user3033423: `self.text.index("insert")` will return the index of the insertion cursor in the form of *line.character* – Bryan Oakley Feb 27 '14 at 23:38