tkinter - Going Back and Forth Between Frames Using Buttons

前端 未结 1 1358
天涯浪人
天涯浪人 2021-01-26 16:19

I need functions, preferably one function, that can go back and forth between pages when the next and back buttons are pressed. I imagine this could be done by assigning boolean

1条回答
  •  鱼传尺愫
    2021-01-26 16:43

    You are very close to having it all working, after some looking myself I couldn't find any (not overly-complicated) way to figure out the top most Frame so it would probably be best to just keep a record of the current position:

    def __init__(self, parent, controller):
    
        ...
    
        self.position = 0 #the index of the pages list
    

    And to get buttonBool to be passed to changePage you can something from here (Tlapička gives the best solution in my eyes since lambda expressions make the lines of code way too long)

    def __init__(self, parent, controller):
        ...
        # button commands don't have an event but sometimes you use these callbacks for both .bind and buttons
        # so having event=None makes it work for both.
        def go_next(event=None):
            self.changePage(True)
    
        next = ttk.Button(innerFrame, text = "Next", command = go_next)
        next.grid(row=2, sticky="E")
    
        def go_back(event=None):
            self.changePage(False)
    
        back = ttk.Button(innerFrame, text = "Back", command = go_back)
        back.grid(row=2, sticky="W")
    
        ...
    

    With these two (and implementing self.position into changePage) you can accomplish what you originally asked, everything below this is the code reviewer in me talking.


    Although using a boolean would work, this strategy of dealing with extra arguments to callbacks lets you pass any argument into changePage so it would probably simplify the conditionals in changePage if it got the change in pages (so 1 or -1):

        def go_next(event=None):
            self.changePage(1)
    
        next = ttk.Button(innerFrame, text = "Next", command = go_next)
        next.grid(row=2, sticky="E")
    
        def go_back(event=None):
            self.changePage(-1)
    
        back = ttk.Button(innerFrame, text = "Back", command = go_back)
        back.grid(row=2, sticky="W") 
    
        #this is for the last suggestion
        self.nextButton = next
        self.backButton = back
        ...
    

    then changePage could look like this although I'm not sure what would happen to self.position if you changed to an invalid page:

    def changePage(self,change):
        pages = [self.pageOne,self.pageTwo,self.pageThree]
        new_position = self.position + change
        if (new_postion < 0) or (new_postion <= len(pages)):
            show_frame(BlankPage)
            #not sure how you would handle the new position here
        else:
            pages[new_position].tkraise()
            self.position = new_position
    

    Even better, if you keep a reference to the next and back buttons you can config them to indicate that it is the end/beginning:

    def changePage(self,change):
        pages = [self.pageOne,self.pageTwo,self.pageThree]
        new_position = self.position + change
        if (0 <= new_postion < len(pages)):
            pages[new_position].tkraise()
            self.position = new_position
        else:
            show_frame(BlankPage)
    
        if new_position+1 >= len(pages):
            self.nextButton.config(text="End") #, state=tk.DISABLED)
        else:
            self.nextButton.config(text="Next") #, state=tk.NORMAL)
    
        if new_position-1 < 0:
            self.backButton.config(text="First") #, state=tk.DISABLED)
        else:
            self.backButton.config(text="Back") #, state=tk.NORMAL)
    

    that way you would know when you reached the end even if there isn't indication from the contents. (or you could disable the buttons to prevent going past)

    0 讨论(0)
提交回复
热议问题