2

It seems I'm hitting some kind of preset maximum scrollable canvas size, that I didnt know about...

I've written a simple bare-bones iTunes replacement in Tkinter.

Since I like the album cover view, an album needs at least 200x200 px size, and I have A LOT of albums (~1600), it follows that I need a lot of space.

But I discovered that above a height ~ 35000px the window is unable to show them.

Here I written a sample code of it - it needs ImageMagick's convert, and around 15sec to run on my machine. You can see that the window only shows 163 of the 170 squares...

from Tkinter import *

import subprocess

def main():

    root = Tk()
    root.geometry("%dx%d+0+0" % (1800,1000))
    cv = Canvas(root)
    vscrollbar = Scrollbar(root, orient=VERTICAL)
    vscrollbar.pack(fill=Y, side=RIGHT)
    vscrollbar.config(command=cv.yview)
    cv.config(yscrollcommand=vscrollbar.set)
    cv.configure(scrollregion=(0,0, 4000, 50000))      
    cv.pack(side=LEFT, fill=BOTH, expand=TRUE)    
    fcv=Frame(root)
    cv.create_window(0, 0, anchor = "nw", window=fcv)    


    memimages=[]    
    for row_index in range(170):
        a=subprocess.Popen("convert -size 200x200 -pointsize 22  -gravity center label:%d test.gif" % row_index, shell=True,
                           stdout=subprocess.PIPE,stderr=subprocess.PIPE) 
        output, errors = a.communicate()
        iconimage = PhotoImage(file="test.gif")
        b=Button(fcv,image=iconimage)
        memimages.append(iconimage)             
        b.grid(row=row_index, column=0, sticky=N+S+E+W)  

    mainloop()

main()
7
  • 1
    It's true that Tkinter has some technical limitations. That said, how useful is it to have everything in one page in a giant list? Mar 16, 2017 at 9:35
  • I had a laugh at the video... but seriously, I'm just replicating what any itunes-like program does in cover view. At 200x200px, my screen shows ~ 12x7=84 albums at a time, so I only have to scroll 20 screens to see them all - that's hardly 'giant'
    – alessandro
    Mar 16, 2017 at 10:22
  • Again, Tkinter has some technical limitations. It's not going to perform like a native application. And what's wrong with having multiple pages, like most websites do for search results? Also again, you might consider a more useful interface, which might include searching, sorting, and filtering. Mar 16, 2017 at 10:29
  • Search/Sort/Filter are on the todo list of course. In any case that's exactly why I'm doing it: I personally dont like the alternatives. Anyway we're straying from the question: are we really sure that's a Tkinter limitation? Is it really hardcoded somewhere?
    – alessandro
    Mar 16, 2017 at 11:04
  • I don't know, but it's not the first time I've seen Tkinter struggle like this. Mar 16, 2017 at 11:18

2 Answers 2

2

I modified your code to show an image at specific pixel height locations, e.g. one at y=0, one at y=32000 and one at y=50000. The canvas is able to traverse from 0 all the way to 50,000 pixel height, and we can see the images as expected.

This means the canvas is able to scroll all the way to y=50000 pixels and the problem lies not with pixel height limitation of canvas but I am guessing it could be with the manner the button is placed into the frame of the canvas window or the placement of frame in the canvas window or the placement of the canvas window itself into the canvas.

You can run this revised code to see what I mean. Scroll all the way to the bottom. Hope this gives you more insight to troubleshoot your code.

from Tkinter import *

def main():

    root = Tk()
    root.geometry("%dx%d+0+0" % (1800,1000))
    cv = Canvas(root)
    vscrollbar = Scrollbar(root, orient=VERTICAL)
    vscrollbar.pack(fill=Y, side=RIGHT)
    vscrollbar.config(command=cv.yview)
    cv.configure(yscrollcommand=vscrollbar.set)
    cv.configure(scrollregion=(0,0, 4000, 50000))      
    cv.pack(side=LEFT, fill=BOTH, expand=TRUE)
    iconimage = PhotoImage(file="monkey.gif")
    testimage = cv.create_image(300, 0, image=iconimage)
    testimage1 = cv.create_image(300, 32000, image=iconimage)
    testimage2 = cv.create_image(300, 50000, image=iconimage)
    mainloop()

main()

Update: After further testing, it does seems there is a limitation on the display height of the window formed by the Canvas.create_window() method. I added the code below, just before mainloop(), which attempts to create buttons and labels with image of 100x100 pixels. The max. no. of rows of buttons that could be displayed was 316+ while max. no. of rows of labels that could be displayed was 322+. If buttons and labels were created together, the max. no. of row that could be displayed was 316+. My conclusion appears to be identical to yours.

Sorry to not have been able to answer your question. However, I hope to support you with my answer, and recommend someone more knowledgeable explains why this behaviour is the case.

fcv=Frame(cv)
cv.create_window(0, 0, anchor = "nw", window=fcv)    
iconimage = PhotoImage(file="monkey100.gif") # Image dimension is 100x100 pixels
for row_index in range(340):
    b=Button(fcv,image=iconimage)
    b.grid(row=row_index, column=0, sticky=N+S+E+W)
    lb=Label(fcv,text=str(row_index), image=iconimage, compound=LEFT)
    lb.grid(row=row_index, column=1, sticky=N+S+E+W)
0

I found a way out the problem. the limit seens to come from create_window iteself. So i create multiples windows and this works fine...

from tkinter import *
from PIL import Image, ImageTk, ImageDraw



root = Tk()
vsb = Scrollbar(root, orient=VERTICAL)
vsb.grid(row=0, column=1, sticky=N+S)
hsb = Scrollbar(root, orient=HORIZONTAL)
hsb.grid(row=1, column=0, sticky=E+W)
c = Canvas(root,yscrollcommand=vsb.set, xscrollcommand=hsb.set)
c.grid(row=0, column=0, sticky="news")
vsb.config(command=c.yview)
hsb.config(command=c.xview)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

w, h = 200,350
image={}

for i in range(0,200):
    fr = Frame(c)
    c.create_window(2, i*(h+2),  window=fr)
    image[i]=Image.new ('RGB', (w, h))
    draw = ImageDraw.Draw(image[i])
    draw.rectangle ((0,0,w,h), fill = (20,20,20) )
    draw.text ((1,1), str(i), (255,255,255))
    image[i]=ImageTk.PhotoImage(image[i])
    btn=Button(fr, image=image[i])
    btn.pack()   
    fr.update_idletasks()
    
c.config(scrollregion=c.bbox("all"))
root.mainloop()
quit()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.