tkinter的最大画布尺寸? [英] tkinter maximum canvas size?

查看:93
本文介绍了tkinter的最大画布尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我要达到某种预设的最大可滚动画布大小,这是我所不知道的...

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

我写了一个简单的准系统在Tkinter中替换iTunes。

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

由于我喜欢相册的封面视图,因此相册至少需要200x200 px的大小,而且我有很多相册(〜1600张),随之而来的是我需要很多空间。

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.

但是我发现,高度超过35000px时,窗口无法显示它们。

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

在这里我写了一个示例代码-它需要ImageMagick的 convert ,大约需要15秒才能在我的计算机上运行。
您可以看到该窗口仅显示170个正方形中的163个...

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()


推荐答案

我修改了代码,以在特定像素高度的位置显示图像,例如y = 0时一个,y = 32000时一个,y = 50000时一个。画布能够从0一直移动到50,000像素高度,我们可以看到预期的图像。

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.

这意味着画布能够一直滚动到y = 50000像素,问题不在于画布的像素高度限制,但我想可能是按钮放置在画布窗口的框架中的方式,框架在画布窗口中的放置或画布窗口本身在画布中的放置方式。

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()

更新:经过进一步测试,似乎由 Canvas.create_window()方法。我在 mainloop()之前添加了以下代码,该代码试图创建具有100x100像素图像的按钮和标签。最高没有。可显示的按钮行数为316+,而最大没有。可显示的标签行数为322+。如果按钮和标签是一起创建的,则最大没有。可显示的行数是316+。我的结论似乎与您的结论相同。

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)

这篇关于tkinter的最大画布尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆