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

查看:23
本文介绍了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 像素大小,而且我有很多专辑(~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天全站免登陆