tkinter - Going Back and Forth Between Frames Using Buttons

五迷三道 提交于 2019-12-02 08:40:55
Tadhg McDonald-Jensen

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!