为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕少森 2022-03-09 21:24:05
我有一个名为self.canvas_input_frame的画布,其中的画布名为self.canvas_input_image. self.canvas_input_image是实际显示图像的位置。我已将子元素垂直和水平滚动条附加到self.canvas_input_frame. 我的 GUI 如下图所示:水平滚动条不完全从西向东延伸。如何解决?下面是我的代码: 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"))
查看完整描述

1 回答

?
暮色呼如

TA贡献1853条经验 获得超9个赞

这里的问题是包装小部件的顺序。如果您更改包装元素的顺序,如下所示,那么它可以获得您预期的视图。


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 包装在“左”中,则任何其他元素只能放在其“右”的另一侧(而不是左、上或下)。类似的逻辑也适用于其他方面。


查看完整回答
反对 回复 2022-03-09
  • 1 回答
  • 0 关注
  • 141 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信