3

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

John Crow
  • 927
  • 3
  • 13
  • 26
  • 2
    I don't understand the problem. It looks like to me each canvas spans three columns. The columns are unequal in width so the columns are different sizes, but that seems intention since you're giving weights to only a couple of columns. By the way, it would be much easier to answer this question if you removed the dependency on matplotlib. Just use a blank canvas. – Bryan Oakley Jun 20 '17 at 22:49
  • Ah, I misunderstood the way `columnconfigure` was working. I thought it was applying the weight to each of the columns. If you add an answer to that effect I will accept. Thanks. – John Crow Jun 20 '17 at 23:02

1 Answers1

2

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)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685