user1956185
user1956185

Reputation: 389

Expand a listbox over 2 columns Tkinter

I have the following interface: 3 frames from which 2 are on the left-hand, frame1 and frame2 and another one which is on the right, frame3.

I want to create another frame, frame4 which will have the label Output and under it there should be a Listbox. I want that both these widgets to span over the previous frames, each being places in a cell by using the grid manager.

Also I am not sure if Listbox is the widget I should be using. I want something which will contain the output of the program I will run through my application. I also thought of Entry but I need something in which I can display more than one line.

This is the code I have so far:

from Tkinter import *

root = Tk()

frame1 = Frame(root)
frame1.grid(row=0,column=0)

frame2 = Frame(root)
frame2.grid(row=1,column=0)

frame3 = Frame(root)
frame3.grid(row=0,column=1)

frame4 = Frame(root)
frame4.grid(row=2,columnspan=2)
l5 = Label(frame4, text='Output:').grid(row=2,columnspan=2)
output = Listbox(frame4, height=5)
#output.grid(row=2,column=0,columnspan=2)
#output.pack(side=LEFT,  fill=BOTH, expand=1)

root.mainloop()

I managed to make the label to span across the other frames, but when I uncommented either of the last 2 lines, the interface didn't open and the program just froze.

How can I solve this?

Upvotes: 0

Views: 4934

Answers (2)

L.Clarkson
L.Clarkson

Reputation: 492

If you use both .grid() and .pack() Tkinter (python 2.x) or tkinter (python 3.x) will happily spend the rest of your life trying to find a way to satisfy both, you must use one within the same window or Frame, see http://www.openbookproject.net/py4fun/gui/tkPhone.html

Upvotes: 0

Marcin
Marcin

Reputation: 238111

I slightly ammended your code:

from tkinter import * # for python 2.7 use Tkinter

root = Tk()


frame1 = Frame(root, bg='red', height=20)
frame1.grid(row=0,column=0, sticky=W+E)

frame3 = Frame(root, bg='blue', height=20)
frame3.grid(row=0,column=1, sticky=W+E)

frame2 = Frame(root, bg='green',  height=20)
frame2.grid(row=1,column=0, sticky=W+E)



frame4 = Frame(root)
frame4.grid(row=2,columnspan=2, sticky=E+W)

l5 = Label(frame4, text='Output:', bg='orange').grid(row=0, column=0, sticky=E+W)
output = Listbox(frame4, height=5, width=50)
output.grid(row=1,column=0)
#output.pack(side=LEFT,  fill=BOTH, expand=1)

root.mainloop()

This results in:

enter image description here

So basically what I did was to add bg, height and sticky parameters to frames and label to easily visual what is happening and how they frames are laid out. Also I modified grid parameters listbox and label.

Hope this is what you are after, or it will help you to get there.

Upvotes: 2

Related Questions