Tkinter grid manager: columns are ignored

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

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

      

Where B represents one of the 9 buttons. The code below is close, but it seems to me that canvases A and C are just ignoring the argument columnspan=3

, whereas canvas B is using 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 similar question does not solve this problem as there are no empty columns here.

+3


source to share


1 answer


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

.

For example:



root.columnconfigure((0,1,2,3,4,5,6,7,8), weight=1)

      

+2


source







All Articles