Scrollable Toplevel Window (tkinter)

房东的猫 提交于 2019-12-25 08:22:46

问题


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:


回答1:


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

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.

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


来源:https://stackoverflow.com/questions/42513112/scrollable-toplevel-window-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!