可滚动的顶层窗口(tkinter) [英] Scrollable Toplevel Window (tkinter)

查看:539
本文介绍了可滚动的顶层窗口(tkinter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python还是很陌生,并且已经简化了一个小程序.在该程序中,用户可以打开一个顶级窗口作为弹出窗口,该窗口将地图显示为图像文件.我设法在其中添加了一个滚动条,并使图像可滚动.

I'm pretty new to Python and have delopped a little program. In that program the user is able to open a Toplevel window as a popup which shows a map as an image file. I've managed to add a scrollbar to it and make the image scrollable.

使用滚动条的原因是支持不同的屏幕分辨率,因此,如果显示的图像太大,则用户可以滚动弹出窗口的内容.

The reason for the scrollbar is to support different screen resolutions so that if the shown image is too big, the user can scroll the content of the popup.

我现在要确保在弹出窗口更改大小或由于缺少屏幕大小而没有完全拉伸时,滚动条更改大小.到目前为止,只要缩小窗口大小,滚动条就会消失.

I now would like to ensure that the scrollbar changes size, when the popup window changes size or isn't completely stretched because of missing screen size. So far the scrollbar disappears as soon as you shrink the window size.

这是打开弹出窗口的功能:

Here is my function that opens the popup window:

推荐答案

在使用网格系统布局小部件的情况下,您需要.rowconfigure().columnconfigure()方法来获得所需的内容.

You need .rowconfigure() and .columnconfigure() methods to get what you want given you are using the grid system to layout your widgets.

为进一步帮助您,我已注释掉部分代码.尽管您的代码显示了图像,但这不是在Canvas中创建图像的正确方法.您的图像是在位于画布顶部的框架中创建的.因此,尽管您可以看到图像和滚动条,但您也将无法滚动图像.请改用我给您的正确代码.

To help you further, I have commented out a section of your codes. Although your code displayed an image, it isn't the correct way to create an image in a Canvas. Your image was created in a Frame that sat on top of the Canvas. As such, you will not be able to scroll your image either although you can see the image and the scrollbar. Use the correct code I gave you instead.

最后评论.将来一定要学习提供简化的完整代码,以便您更快地获得帮助.您可以在此处了解更多有关mcve的信息.

Last comment. Do learn to provide a simplified complete code in the future so that you can attract help quicker. You can read more about about mcve here.

from tkinter import *

class App(Frame):
    def __init__(self, parent, *args, **kwargs):
        Frame.__init__(self, parent, *args, **kwargs)
        header = "Toplevel"
        pfad = "NYCGifathon24-3.png" # change this to your image name
        source = "Canvas Image"
        self.karte(pfad,header,source)

    def karte(self, pfad,header,source): #added 'self' 
        popup = Toplevel()
        popup.title(header)

        ksbar=Scrollbar(popup, orient=VERTICAL)
        ksbar.grid(row=0, column=1, sticky="ns")

        popCanv = Canvas(popup, width=600, height = 800,
                         scrollregion=(0,0,500,800)) #width=1256, height = 1674)
        popCanv.grid(row=0, column=0, sticky="nsew") #added sticky

        ksbar.config(command=popCanv.yview)
        popCanv.config(yscrollcommand = ksbar.set)

        ## Commented codes are inappropriate.
        ## Wrong way to create an image in Canvas.
        ## Your scrollbars will not be able to scroll the image either
        #kframe=Frame(popCanv, width=600, height = 800) 
        #kframe.grid(row=0, column=0)
        #img = PhotoImage(master=kframe, file=pfad)
        #imglabel = Label(kframe, image = img)
        #imglabel.image = img
        #imglabel.grid()
        self.img = PhotoImage(file=pfad) #amended
        image = popCanv.create_image(300, 400, image=self.img) #correct way of adding an image to canvas
        popCanv.create_text(420,790,text=source)

        popup.rowconfigure(0, weight=1) #added (answer to your question)
        popup.columnconfigure(0, weight=1) #added (answer to your question)

        #popup.mainloop()

if __name__ == "__main__":
    root = Tk()
    app = App(root)
    root.mainloop()

这篇关于可滚动的顶层窗口(tkinter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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