简体   繁体   English

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

[英]Python Tkinter - Extend Scrollbar from East to West Border of the Frame Canvas

I have a canvas named self.canvas_input_frame with a canvas within called self.canvas_input_image . 我有一个名为帆布self.canvas_input_frame一个名为内画布self.canvas_input_image self.canvas_input_image is where the image is actually displayed. self.canvas_input_image是实际显示图像的位置。 I have attached a child element vertical and horizontal scrollbars to self.canvas_input_frame . 我已将一个子元素垂直和水平滚动条附加到self.canvas_input_frame My GUI looks like the image below: 我的GUI如下图所示:

在此处输入图片说明

The horizontal scrollbar does not extend from west to east completely. 水平滚动条不会完全从西向东延伸。 How to fix it? 如何解决? Below is my code: 下面是我的代码:

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

Here the issue is the order of packing widgets. 这里的问题是包装小部件的顺序。 If you change the order of packing the elements like below then it can get you expected view. 如果您像下面那样更改元素的打包顺序,则可以获得预期的视图。

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

One way to understand pack method is that if you pack widget1 in 'left' then any other elements can only fit on its opposite side that is 'right'(not left, up or down). 一种理解pack方法的方法是,如果将widget1打包在“左”中,则任何其他元素只能放在其相对的“右”侧(而不是左,上或下)。 Similar logic applies for other sides as well.. 类似的逻辑也适用于其他方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM