Hi! I am using a label widget in order to display a n rows and m columns matrix. My program allows repeated generation of a random matrix of integer (of arbitrarily number of rows and columns) - I am observing these matrices by representing them in a Label type widget.
The problem is that when I generate and represent a bigger matrix (let's say 10x10) and after that a smaller one (5 x 5) the smaller one is superimposed over the bigger one. I mean - I can distinguish the elements of the smaller matrix which is bordered by the uncovered elements of the bigger matrix. Also - needles to say - that area of the window doesn't shrink to fit the smaller matrix - it remains at the sizes required to represent the bigger matrix. My guess is that the previous Label widget (representing the 10x10 matrix) isn't override by the newer one (5x5 - smaller matrix) as I intended - instead they coexist somehow.

Please tell me how to avoid the Label widget superpositions - also how to shrink the parent widget in order to fit the label.

Bellow is the code:

from rand_mat import RMO
from Tkinter import *
import functools

version=" 0.00a "

def showThem(up):
    vars=[]
    vars.extend([int(up.rows.get()), int(up.cols.get()), int(up.lmin.get()), int(up.lmax.get())])

    low=Lower(vars, master=up)
    low.grid(column=0, columnspan=8, row=1) 

class Lower(Frame):
    def __init__(self, vars, master=None):
        Frame.__init__(self, master)
        self.grid()

        rmatrix=RMO(vars[0], vars[1], vars[2], vars[3])

        self.lbl=Label(self, text="\n".join([(str(i)[1:-1]).replace(',', ' ') for i in rmatrix.data]),
                       justify=CENTER)
        self.lbl.grid(column=0, row=1)

class Upper(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()

        self.rowlabel=Label(self, text="rows")
        self.rowlabel.grid(column=0, row=0)
        self.rows=Entry(self, width=3)
        self.rows.grid(column=1, row=0)

        self.collabel=Label(self, text="cols")
        self.collabel.grid(column=2, row=0)
        self.cols=Entry(self, width=3)
        self.cols.grid(column=3, row=0)

        self.lminlabel=Label(self, text="min")
        self.lminlabel.grid(column=4, row=0)
        self.lmin=Entry(self, width=3)
        self.lmin.grid(column=5, row=0)

        self.lmaxlabel=Label(self, text="max")
        self.lmaxlabel.grid(column=6, row=0)
        self.lmax=Entry(self, width=3)
        self.lmax.grid(column=7, row=0)
   
root=Tk()

root.title("Random matrix operations - version"+version)

menubar=Menu(root)
root.config(menu=menubar)
mainmenu=Menu(menubar)
menubar.add_cascade(label="Main", menu=mainmenu)

up=Upper()
up.pack(side=TOP, fill=X)

mainmenu.add_command(label="Generate", command=functools.partial(showThem, up=up))
mainmenu.add_command(label="Reset", command=None)
mainmenu.add_command(label="Exit", command=root.quit)

unaryopmenu=None

binaryappmenu=None

root.mainloop()

and the additional code from rand_mat (required for the generation of random elements)

class RandMat:
    """
Simple class whose instances generate a matrix of random integers.

Usage:
       
x=RandMat(3, 4, -1, 10)

will generate a 3x4 integer matrix with each integer
randomly generated between -1 and 10 integer limits.

The x.data member will be a list with 3 member, each member beeing
a list with four random integers.
"""
    
    def __init__(self, rows, cols, l1, l2):
        self.rows=rows
        self.cols=cols
        self.data=[]
        self.__generate(l1, l2)
        
    def __generate(self, l1, l2):
        import random
        for i in range(0, self.rows):
            self.data.append([random.randint(l1, l2) for i in range(0, self.cols)])

    def __str__(self):
        str_m=""
        
        for i in range(0, self.rows):
            str_r=""
            for j in range(0, self.cols):
                str_r=str_r+str((self.data[i])[j])+" "
            str_m=str_m+str_r+"\n"
            
        return str_m

class RMO(RandMat):
    """ \nThis class extends the RandMat class type by adding \n extra operations."""
        
    def transpose(self):
        import copy
        tmp=copy.deepcopy(self)
        for i in range(0, self.rows):
            for j in range(i, self.cols):
                if i != j :
                    a=(self.data[i])[j]
                    (tmp.data[i])[j]=(self.data[j])[i]
                    (tmp.data[j])[i]=a
        return tmp

Thanks

PTS

Try either destroy or withdraw on the old label and see if it does what you want.

Edited 6 Years Ago by woooee: n/a

Ok - Thank you - I've tried the .destroy() method and it worked. I already tried the solutions you proposed - this time however I declared that object related to the widget as global - and now it works. Thanks

This question has already been answered. Start a new discussion instead.