Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid manager in Tkinter: columnspan is ignored

I am trying to get a specific layout of a window using Tkinter's grid manager. The desired layout is this:

_______________________________________________________
|                 |                 |                 | 
|                 |                 |                 | 
|    canvas A     |    canvas B     |    canvas C     | 
|                 |                 |                 | 
|                 |                 |                 | 
|------------------------------------------------------
|  B0 |  B1 |  B2 |  B3 |  B4 |  B5 |  B6 |  B7 |  B8 |   
| ____|_____|_____|_____|_____|_____|_____|_____|_____|

Where B represents one of 9 buttons. The code below gets close, but it looks to me that canvases A and C simply ignore the columnspan=3 argument whereas canvas B uses it correctly.

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import Tkinter as Tk

def generate_window():
    root = Tk.Tk()
    root.rowconfigure((0,1), weight=1, minsize=35)
    root.columnconfigure((0,8), weight=1, minsize=200)

    titles = ['A', 'B', 'C']    
    for t, n in zip(titles, xrange(1, 4)):
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot([1,2,3,4], [1,4,9,16], 'ro')
        ax.set_title(t)
        canvas = FigureCanvasTkAgg(fig, master=root)
        canvas.get_tk_widget().grid(row=0, column=(n-1)*3, columnspan=3, sticky='NSEW')

    for x in xrange(0, 9):
        Tk.button = Tk.Button(master=root, text=str(x), command= lambda x=x:button(x))
        Tk.button.grid(row=1, column=x, sticky='NSEW')
    Tk.mainloop()

def button(button_number):
    print button_number



generate_window()

This similarly phrased question does not solve this problem, since there are no empty columns here

like image 993
John Crow Avatar asked Jun 06 '26 11:06

John Crow


1 Answers

You are only applying a column weight to a couple of columns, so you end up with unequal column widths. If you want all of the columns to be the same size, the simplest solution is to give them all equal weights and/or use the uniform option.

For example:

root.columnconfigure((0,1,2,3,4,5,6,7,8), weight=1)
like image 156
Bryan Oakley Avatar answered Jun 08 '26 02:06

Bryan Oakley