Home > front end >  keep the aspect ration of main window on tkinter
keep the aspect ration of main window on tkinter

Time:09-28

Creating an app with tkinter that needs some aspect ratios to work petty well, for that I need to keep the ratio of the main window, but if I bind a so when the window refreshes it goes into an infinite loop, any way to check this and only really do when someone gives resize in the app?

    def appBinds(self):
        def keepRatio(e):
            width = int(e.width)
            height = int(width/0.75)
            self.geometry(f'{width}x{height}')
            self.unbind('<Configure>')
            print(e)
            
        self.bind('<Configure>', keepRatio)

CodePudding user response:

To avoid the infinite loop issue, you need to call self.geometry(...) only when the height is not the expected height as below:

    def appBinds(self):
        def keepRatio(e):
            if e.widget is self:
                height = int(e.width / 0.75)
                if e.height != height:
                    self.geometry(f"{e.width}x{height}")

        self.bind('<Configure>', keepRatio)
  • Related