你已经非常接近让一切正常工作了,在我自己看了一些之后,我找不到任何(不是过于复杂的)方法来找出最上面的Frame
所以最好只记录当前位置:
def __init__(self, parent, controller):
...
self.position = 0 #the index of the pages list
并得到buttonBool
传递给changePage
你可以来自这里的东西 https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter(Tlapička 给出了我眼中最好的解决方案,因为lambda
表达式使代码行太长)
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")
...
有了这两个(并实施self.position
into changePage
)你可以完成你最初要求的,下面的所有内容都是代码审查员 http://codereview.stackexchange.com在我说话的时候。
尽管使用布尔值可行,但这种处理回调的额外参数的策略可以让您通过any论证成changePage
所以它可能会简化条件changePage
如果页面发生变化(因此 1 或 -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
可能看起来像这样,虽然我不确定会发生什么self.position
如果您更改为无效页面:
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
更好的是,如果您保留对next
and back
按钮你可以config
它们表明这是结束/开始:
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)
这样,即使内容没有指示,您也会知道何时到达终点。 (或者您可以禁用按钮以防止过去)