你需要.rowconfigure() 和.columnconfigure() 方法来获得你想要的,因为你正在使用网格系统来布局你的小部件。
为了进一步帮助您,我已注释掉您的一段代码。尽管您的代码显示了图像,但这不是在 Canvas 中创建图像的正确方法。您的图像是在位于画布顶部的框架中创建的。因此,尽管您可以看到图像和滚动条,但您也无法滚动图像。请改用我给你的正确代码。
最后的评论。以后一定要学习提供简化的完整代码,以便更快地获得帮助。您可以阅读有关 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()