Two Tkinter questions

Rune Hansen rune.hansen at vivenus.no
Sat May 18 05:13:21 EDT 2002


Hello Mike, 

Q1:
To make a grid widget behave like when packed you'll have to use column and 
row configure.
--
label = Label(frame,text="Text")

label.grid(row=0,column=0,sticky=N+E+S+W)
frame.rowconfigure(0,weight=1)
frame.columconfigure(0,weight=1)
--
This will make the widget use the maximum vailible space in the frame.


Q2:
A bit more tricky. I've adaped this code from MCScrolledListBox.py,v 1.3 
written by Dough Hellman. (http://sourceforge.net/projects/pmwcontribd/) 
Helmanns code is much more complete, but I also does some stuff i didn't 
want(like list headers and the selection spanning the lists).
My example is rather verbose, I found it easier than to strip my code..
--
#Scrolled lists
[class definition]

# Initialize a list for "list"
self.lists=[]

#Create the scrollbar
self.scrollbar = Scrollbar(self.frame, orient=VERTICAL)

#Create three lists
self.list1 = Listbox(self.frame,
                     bd=0,
                     highlightcolor="white",
                     highlightthickness=0,
                     fg="red",
                     bg="white",
                     width=5,
                     yscrollcommand=self._yset
                     )
self.lists.append(self.list1)
self.list2 = Listbox(self.frame,
                     bd=0,
                     highlightcolor="white",
                     highlightthickness=0,
                     fg="#9CC6CD",
                     bg="white",
                     width=1,
                     yscrollcommand=self._yset
                     )
self.lists.append(self.list2)
self.list3 = Listbox(self.frame,
                     bd=0,
                     highlightcolor="white",
                     highlightthickness=0,
                     fg="black",
                     bg="white",
                     yscrollcommand=self._yset
                     )
self.lists.append(self.list3)

# Bind selection command to list3.
self.list3.bind("<ButtonRelease-1>",self.selectHandler)
self.list3.bind("<Key-space>",self.self.selectHandler)
self.list3.bind("<Key-Return>",self.selectHandler)

# Configure scrollbar 
self.scrollbar.config(command=self._yview)

# Grid the lists and scrollbar
self.list1.grid(row=0,column=0,sticky=W)
self.list2.grid(row=0,column=1,sticky=W)
self.list3.grid(row=0,column=2,sticky=N+E+S+W)
self.scrollbar.grid(row=0,column=3,sticky=N+S)

# Make list3 expand the amount of availible space in the frame 
self.frame.rowconfigure(0, weight = 1)
self.frame.columnconfigure(2, weight = 1)

.
.
.

def selectHandler(self):
        # ...
        pass
# These functions are unchanged from Hellmans example
def _yset(self, *args):
    #print '_yset', args
    apply(self.scrollbar.set, args)
    for lb in self.lists:
        lb.yview_moveto(args[0])
    return
    
def _yview(self, *args):
    for lb in self.lists:
        apply(lb.yview, args)
    return
--
This will make the lists scroll in unison even when using the mousewheel :-) 
With a bit focus() tinkering this also works on Windows.


regards

/rune

Mike Callahan wrote:

> Question #1
> 
> I want a text widget to completely fill its space. I can do this in
> tkinter using the pack geometery manager:
> text.pack(fill='both', expand=1)
> If I resize the window, the the text widget continues to fill the window,
> however if I use grid:
> text.grid(sticky='nsew')
> the text widget stays the same size if I make the window larger. Can't
> grid do the same thing that pack can do?
> 
> Question #2
> 
> I want to make a horizontal scroll bar scroll two text widgets at the same
> time. I know how to connect a scrollbar to a text widget, but is there a
> way to connect one scrollbar to two widgets?
> 
> Thanks,
> Mike Callahan





More information about the Python-list mailing list