我有一个名为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"))
答案 0 :(得分: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装在“ left”中,那么其他任何元素只能放在其对置的“ right”一侧(而不是left,up或down)。类似的逻辑也适用于其他方面。