1

I am new to GUI programming and tkinter in specific. I'm trying to design a small interface with a few frames that will display different information. I've made a frame and embedded a canvas in it, however I have run into difficulties trying to bind a resize method to "Configure" so the canvas will resize when the window is resized by the user. I modified the resize method (and associated class) from this question.

Upon running the code the event will be triggered in an endless loop. I feel that I must be making some stupid mistake but cannot find it. One oddity I've identified is that the attributes of the resulting Canvas object increment the height and width of the passed parameters by 4. I am not sure if is causing the loop, but cannot find any discernible reason why this is happening.

Any help is much appreciated.

#class for main window
class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)                 
        self.master = master
        self.init_window()

    #method for initializing frames, only canvas frame shown here
    def init_window(self):
        self.Lsidebar = Frame(self.master, bg="white", borderwidth = 3)
        self.Lsidebar.grid(row = 0, column = 0, rowspan = 2)
        self.Lside_canvas = ResizingCanvas(self.Lsidebar, width = 120, height = 600)
        self.Lside_canvas.grid(row = 0, column = 0)

class ResizingCanvas(Canvas):
    def __init__(self,parent, width, height):
        Canvas.__init__(self,parent, width=width, height=height)
        self.height = self.winfo_reqheight()
        self.width = self.winfo_reqwidth()
        self.bind("<Configure>", self.on_resize)

    #method for resizing canvas
    def on_resize(self, event):
        self.width = event.width
        self.height = event.height
        self.config(width=self.width, height=self.height)


root = Tk()
#sets some initial sizes for placing frames
root.columnconfigure(0, weight=1, minsize = 120)
root.columnconfigure(1, weight=7, minsize = 500)
root.rowconfigure(0, weight=1, minsize = 400)
root.rowconfigure(1, weight=1, minsize = 200)
app = Window(root)
root.mainloop()
Community
  • 1
  • 1
  • You have to use the option highlightthickness=0 and it works but I cannot tell you why. I would say the original code is very brittle. – Jannick Mar 15 '17 at 16:59
  • Thank you, that did stop it from resizing itself and corrected the additional 4 pixels to height/width. I also found that removing the frame the canvas was in and simply placing the canvas in the grid allowed it to resize with the grid weight configurations. – Corey Schimpf Mar 27 '17 at 17:21

0 Answers0