Python Tkinter - 将滚动条从框架画布的东边界延伸到西边界

我有一个名为self.canvas_input_frame的画布,其中的画布名为self.canvas_input_imageself.canvas_input_image是实际显示图像的位置。我已将子元素垂直和水平滚动条附加到self.canvas_input_frame. 我的 GUI 如下图所示:

http://img2.mukewang.com/6228aa8b0001225607640761.jpg

水平滚动条不完全从西向东延伸。如何解决?下面是我的代码:


 def open_file_dialog(self):


    self.filename = filedialog.askopenfilename(initialdir = "C:/Users/alyss/AppData/Local/Programs/Python/Python36/Damaged Text Document Virtual Restoration", title = "Select A File", filetype =  (("png", "*.png"),("jpeg", "*.jpg"),  ("pdf", "*.pdf"), ))



    #Create Canvas Frame for Input Document

    self.canvas_input_frame = tk.Canvas(self.main_canvas,  bg = "blue")

    self.canvas_input_frame.configure(width=456, height=470)       

    self.canvas_input_frame.pack(side="left", padx=10,pady = 10)


    #Load Input Image

    load = Image.open(self.filename)      

    render = ImageTk.PhotoImage(load)

    self.width, self.height = load.size


    self.canvas_input_image = tk.Canvas(self.canvas_input_frame,bg = "green") # how to render image in canvas

    self.canvas_input_image.configure(width=390, height=470)

    self.canvas_input_image.image = render

    self.canvas_input_image.create_image(0,0,anchor="nw",image=self.canvas_input_image.image)

    self.canvas_input_image.pack(side="left")



    self.vsb_canvas_input_frame = tk.Scrollbar( self.canvas_input_frame, orient="vertical", command=self.canvas_input_image.yview)

    self.hsb_canvas_input_frame  = tk.Scrollbar( self.canvas_input_frame, orient="horizontal", command=self.canvas_input_image.xview)

    self.canvas_input_image.config(yscrollcommand=self.vsb_canvas_input_frame.set,xscrollcommand=self.hsb_canvas_input_frame.set)

    self.vsb_canvas_input_frame.pack(side = "right", fill = "y")

    self.hsb_canvas_input_frame.pack(side = "bottom", fill = "x")        

    self.canvas_input_image.config(scrollregion = self.canvas_input_image.bbox("all"))


慕少森
浏览 142回答 1
1回答

暮色呼如

这里的问题是包装小部件的顺序。如果您更改包装元素的顺序,如下所示,那么它可以获得您预期的视图。self.vsb_canvas_input_frame.pack(side = "right", fill = "y")self.hsb_canvas_input_frame.pack(side = "bottom", fill = "x")self.canvas_input_image.pack(side="left")理解pack方法的一种方法是,如果您将 widget1 包装在“左”中,则任何其他元素只能放在其“右”的另一侧(而不是左、上或下)。类似的逻辑也适用于其他方面。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python