Tkinter Frame Resize

后端 未结 1 1893
感动是毒
感动是毒 2020-12-07 03:59

I\'ve been stuck for few days trying to figure out how I can resize the frame in TKInter dynamically using this approach.

class SampleApp(tk.Tk):


    def _         


        
相关标签:
1条回答
  • 2020-12-07 04:26

    Instead of stacking the frames, make sure only one is ever managed by grid at a time. You can do this by calling grid_remove() of the current frame and then grid() on the new frame. Or, being lazy you can call grid_remove() on everything so that you don't have to remember which page is current.

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        for frame in self.frames.values():
            frame.grid_remove()
        frame = self.frames[page_name]
        frame.grid()
    

    Note: the automatic resizing will stop working if you give the main window a fixed size with the geometry method on the root window, or if the user manually resizes the window. This is because tkinter assumes that if something explicitly requests a window size, that size should be honored.

    If you always want the window to resize, you should reset the geometry to an empty string. You can add this as the last statement in the show_frame method:

    frame.winfo_toplevel().geometry("")
    
    0 讨论(0)
提交回复
热议问题