dmnfarrell / tkintertable

A pure Python library for adding tables to a Tkinter application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to autoResizeColumns correctly

MRES1 opened this issue · comments

commented

Hello, I'm working with your wonderful module, thank you form all of your work.!

I have two questions if you can help me out with them I would be really thankful.

¿ How can I correctly use the autoResizeColumns() method ?
well, I'm calling the method but I don't see any difference.

This is a little code snippet:

 tframe = Frame(self)
 tframe.pack(fill=BOTH,padx=25, pady=25)
      
self.table = TableCanvas(tframe,
                            data=data,
                            cellbackgr='#F1EFEF',
                            thefont=('Arial', 12),
                            rowheight=30,
                            bg="red",
                            reverseorder=1,
                            grid_color="black",
                            selectedcolor="gray",
                            multipleselectioncolor="#CCCCFF",
                            cellwidth=180,
                            height = 500
                            )
     
        self.table.show()
        self.table.autoResizeColumns()

I looked at the source code and we can find that this method is basically a call for
adjustColumnWidths() and redrawTable()

In the second place, we can see that I have a cellwidth of 180 and happens that when I run the code some values of the tables are not shown till I click and drag one of the scroll bars. What could ber wrong here?

image

So at this point, I drag one of the scroll bars and the data is shown correctly.
If we change the cellwidth to say 100 data is shown correctly but scroll bars disappear.

I have the same question that I have some problems using the autoResizeColumns(). I have figured it out and rewrote corresponding code in my local files. Here is the resolution (not for everyone!).

autoResizeColumns() is called when you use self.table.show() so you don't need to call it after drawing. The bug actually comes from tkintertable.TableModels.getlongestEntry(). In this method it assumes all characters takes the same horizontal space, which is not always. If you are using Chinese characters or some other Asian language in you table, each of them takes 2 or more times of space of English characters do. I am actually using Chinese so I rewrite tkintertable.TableModels.getlongestEntry() like this

    def getlongestEntry(self, columnIndex):
        """Get the longest cell entry in the col"""

        collist = self.getColCells(columnIndex)
        maxw=5
        for c in collist:
            try:
                w = 0
                for ch in str(c):
                    if '\u4e00' <= ch <= '\u9fff': #most Chinese characters are between those in Unicode
                        w += 2 
                    else:
                        w += 1
                print(w)
            except UnicodeEncodeError:
                pass
            if w > maxw:
                maxw = w
        #print 'longest width', maxw
        return maxw

Hope this helps you. Plus, sincerely thankyou to @dmnfarrell for the wonderful module.

Thanks for the code @ACE0PERFECT. As always I recommend people subclass the Table and/or TableModel and override the methods there.