Item limits on frame?

Post here if you need help with creating a Graphical User Interface in Python.

Item limits on frame?

Postby eslavko » Tue Dec 31, 2013 8:31 am

Hello...
In my program I hit to strange behaviour.
Attached program should create matrix of 800x20 cells. It take long time (10 sec) on my computer to start up but shows only 665x20 matrix. And there is no error shown. In real application I expect the matrix to be around 300x20 so the 665x20 is overloaded, but don't know where is problem and if other computer will behave same. I do the test in ubuntu and winXp(under virtualbox) and behave same.

here is stripped version of my program. Try other matrix size in def matrikaConfig().

and of course Happy new year...

Code: Select all
from Tkinter import *
import time


def matrikaConfig():
    for r in range(800): #There is problem!! This goes up to 665 on my system
        for c in range(20):
            matrika.rowconfigure(r, weight=1)
            matrika.columnconfigure(c, weight=1)
            aa=Canvas(matrika,bg="white", height=50, width=50,highlightthickness=0)       
            aa.create_text(0,0,text='', tags=({'ID':'EMPTY'},'config'))
            aa.grid(row=r,column=c,sticky=N+S+E+W)
            aa.create_line(0,0,100,0,fill="azure2")
            aa.create_line(0,0,0,100,fill="azure2")
            aa.create_text(0,0, text=str(r)+'/'+str(c), fill="blue", anchor=NW)
    matrika.update_idletasks()
    mcanvas.config(scrollregion=mcanvas.bbox("all"))

root = Tk()
root.geometry("700x500+0+0")
popupMenu = Menu(root, tearoff=0)


matrikaf = Frame(root)
matrikaf.pack(side=TOP,expand=True,fill=BOTH)

vscrollbar =Scrollbar(matrikaf)
vscrollbar.grid(row=0, column=1, sticky=N+S)

hscrollbar = Scrollbar(matrikaf, orient=HORIZONTAL)
hscrollbar.grid(row=1, column=0, sticky=E+W)

mcanvas = Canvas(matrikaf, yscrollcommand=vscrollbar.set, xscrollcommand=hscrollbar.set)
mcanvas.grid(row=0, column=0, sticky=N+S+E+W)

vscrollbar.config(command=mcanvas.yview)
hscrollbar.config(command=mcanvas.xview)

matrikaf.grid_rowconfigure(0, weight=1)
matrikaf.grid_columnconfigure(0, weight=1)

# create canvas contents
matrika = Frame(mcanvas)
matrika.rowconfigure(1, weight=1)
matrika.columnconfigure(1, weight=1)
mcanvas.create_window(0, 0, anchor=NW, window=matrika)

t0=time.time()
matrikaConfig()
print 'Startup time %2.2f sec' % (time.time()-t0)

root.mainloop()
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am

Re: Item limits on frame?

Postby wuf » Wed Jan 01, 2014 11:38 am

Hi eslavko

There seems to be a limit which allows a maximum number of widget in a Frame widget. Try the following:
Code: Select all
from Tkinter import *
import time

def matrikaConfig():
    row_count = 0
    for row in range(800):
        matrika = Frame(mcanvas)
        for col in range(20):
            matrika.rowconfigure(row, weight=1)
            matrika.columnconfigure(col, weight=1)
            aa=Canvas(matrika, bg="white", height=50, width=50, highlightthickness=0)       
            aa.create_text(0,0,text='', tags=({'ID':'EMPTY'},'config'))
            aa.grid(row=0,column=col,sticky=N+S+E+W)
            aa.create_line(0,0,100,0,fill="azure2")
            aa.create_line(0,0,0,100,fill="azure2")
            aa.create_text(0,0, text=str(row)+'/'+str(col), fill="blue", anchor=NW)
        mcanvas.create_window(0, row_count, anchor=NW, window=matrika)
        matrika.update_idletasks()
        row_count += matrika.winfo_reqheight()
   
    mcanvas.config(scrollregion=mcanvas.bbox("all"))

root = Tk()
root.geometry("700x500+0+0")
popupMenu = Menu(root, tearoff=0)


matrikaf = Frame(root)
matrikaf.pack(side=TOP,expand=True,fill=BOTH)

vscrollbar =Scrollbar(matrikaf)
vscrollbar.grid(row=0, column=1, sticky=N+S)

hscrollbar = Scrollbar(matrikaf, orient=HORIZONTAL)
hscrollbar.grid(row=1, column=0, sticky=E+W)

mcanvas = Canvas(matrikaf, yscrollcommand=vscrollbar.set, xscrollcommand=hscrollbar.set)
mcanvas.grid(row=0, column=0, sticky=N+S+E+W)

vscrollbar.config(command=mcanvas.yview)
hscrollbar.config(command=mcanvas.xview)

matrikaf.grid_rowconfigure(0, weight=1)
matrikaf.grid_columnconfigure(0, weight=1)

# create canvas contents
#matrika = Frame(mcanvas)
#matrika.rowconfigure(1, weight=1)
#matrika.columnconfigure(1, weight=1)
#mcanvas.create_window(0, 0, anchor=NW, window=matrika)

t0=time.time()
matrikaConfig()
print 'Startup time %2.2f sec' % (time.time()-t0)

root.mainloop()

wuf ;)
wuf
 
Posts: 43
Joined: Fri Feb 08, 2013 6:42 am

Re: Item limits on frame?

Postby eslavko » Wed Jan 01, 2014 4:38 pm

This works and it's faster too. (2.5 sec instead 10).
If I understand your changes you put each row in own window.
The line mcanvas.create_window(0, row_count, anchor=NW, window=matrika) confuse me. Is there new window is created or just old one updaed?
nad variable row_count seems missleading for me. It's seems that holds pixel height tof window?
In this method I don't find the way how to insert row somewhere. So to insert row betwen row 2 and 3 and new order should be
row 0
row 1
row 2
row 3 inserted (other rows are shifted one row down.)
row 3
row 4
row 5
row 6


and at all for my opinion python should throw some error in that case.
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am

Re: Item limits on frame?

Postby wuf » Wed Jan 01, 2014 7:26 pm

eslavko wrote:If I understand your changes you put each row in own window.
This is correct. I create a Frame widget 'matrika'. Put 20 Canvas-Widgets horizontally side by side in that Frame. Finally, I put the Frame-Widget in a Canvas Window-Object.
Of course the Canvas Window-Object is not managed by the Grid-Layout-Manager any more. So you have to place the Canvas Window-Object by calculating the y-position in pixels. The pointer for the y-position is of course 'row_count' which is incremented by the height of the Canvas Window-Object. The height of the Window-Object depends of the height of the Frame which is embedded in that Window-Object. The height of the Frame depends again of the height of the highest Canvas-Widget which are embedded horizontally in that Frame. Of course in our case all embedded Canvas-Widgets have the same height.
eslavko wrote:The line mcanvas.create_window(0, row_count, anchor=NW, window=matrika) confuse me. Is there new window is created or just old one updaed?
Names like matrika and mcanvas are confusing me! Why do you not use english names in stead? Also do not use abbreviated names like 'aa', 'r', 'c'!
The object variable 'matrika' ist update by each loop pass with a new Frame-Widget instance reference. And for each line a new Canvas Window-Object will be created, which will be vertically positioned by means of 'row_count' as y-position. Of course for accessing lines an Canvas-Elements in a line more delicate programming is necessary. Our sample here is just on experimental level :-) For further programming i would suggest to use classes. Try to separate the program in a model- and gui-class.
eslavko wrote:In this method I don't find the way how to insert row somewhere.
Here you have to shift up all lines (Canvas Window-Objects) above the inserted line(s). Requires full understanding of programming with Python and Tkinter.
I hope finally you understand what i mean.

wuf ;)
wuf
 
Posts: 43
Joined: Fri Feb 08, 2013 6:42 am

Re: Item limits on frame?

Postby eslavko » Thu Jan 02, 2014 9:11 am

Thanks for clarification wuf.
As you see I'm new to the python.

thanks again and happy new year

Slavko
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am


Return to GUI

Who is online

Users browsing this forum: dnallov, mckryall and 3 guests