tkinter - 使用按钮

时间:2016-02-25 01:11:33

标签: python oop python-3.x tkinter tk

我需要功能,最好是一个功能,当按下下一个和后退按钮时,它们可以在页面之间来回切换。我想这可以通过将布尔变量分配给后面和下一个按钮(不确定是否可以这样做)来确定是否要进行前进或者退回所有页面的有序列表。需要知道当前提升的帧的索引。索引可用于确定下一页,然后它将被引发。如果当前索引为0或最后一个索引(在本例中为2)并分别按回或下一个,那么您将转到主页类框架,在本例中为BlankPage。

import tkinter as tk
from tkinter import ttk

class Program(tk.Tk):        
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, default = "")
        tk.Tk.wm_title(self, "")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (Add, BlankPage):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(Add)

    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()

class Add(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        innerFrame = tk.Frame(self)
        innerFrame.place(relx=.5, rely=.5, anchor="c", relwidth=1.0, relheight=1.0)

        innerFrame.grid_rowconfigure(1, weight=1)
        innerFrame.grid_columnconfigure(0, weight=1)

        name = tk.Label(innerFrame, text = "User")
        name.grid(row=0, sticky="NE")

        pagename = tk.Label(innerFrame, text = "Label")
        pagename.grid(row=0, sticky="N")

        next = ttk.Button(innerFrame, text = "Next", command = self.changePage)
        next.grid(row=2, sticky="E")

        back = ttk.Button(innerFrame, text = "Back", command = self.changePage)
        back.grid(row=2, sticky="W")

########################################################################################################### 

        self.pageThree = tk.Frame(innerFrame)
        self.pageThree.grid(row=1)

        self.pageThree.grid_rowconfigure(0, weight=1)
        self.pageThree.grid_columnconfigure(0, weight=1)

        pagename = tk.Label(self.pageThree, text = "Page 3")
        pagename.grid(row=0, sticky="N")

########################################################################################################### 

        self.pageTwo = tk.Frame(innerFrame)
        self.pageTwo.grid(row=1)

        self.pageTwo.grid_rowconfigure(0, weight=1)
        self.pageTwo.grid_columnconfigure(0, weight=1)

        pagename = tk.Label(self.pageTwo, text = "Page 2")
        pagename.grid(row=0, sticky="N")

########################################################################################################### 

        self.pageOne = tk.Frame(innerFrame)
        self.pageOne.grid(row=1)

        self.pageOne.grid_rowconfigure(0, weight=1)
        self.pageOne.grid_columnconfigure(0, weight=1)

        pagename = tk.Label(self.pageOne, text = "Page 1")
        pagename.grid(row=0, sticky="N")

########################################################################################################### 

    def changePage(self,buttonBool):
        pages = [self.pageOne,self.pageTwo,self.pageThree]
        #find current raised page and set to variable 'current'
        position = pages.index(current)
        if (postion==0 and buttonBool==False) or (postion==len(pages)-1 and buttonBool==True):
            show_frame(BlankPage)
        elif buttonBool==True:
            pages[position+1].tkraise()
        else:
            pages[position-1].tkraise()


class BlankPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)


app = Program()
app.state('zoomed')
app.mainloop()

changePage函数是我对此的尝试,我将如何完成它?

1 个答案:

答案 0 :(得分:1)

你非常接近让它全部工作,经过一些自我找不到任何(不是过于复杂)的方法来找出最顶层的Frame所以最好只保留当前位置的记录:

def __init__(self, parent, controller):

    ...

    self.position = 0 #the index of the pages list

要将buttonBool传递给changePage,您可以something from here(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实施changePage)你可以完成你最初提出的问题,下面的所有内容都是code reviewer在我说话。

虽然使用布尔值会起作用,但这种处理回调的额外参数的策略允许你将任何参数传递给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
    ...

然后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

更好的是,如果你保留对nextback按钮的引用,你可以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)

即使没有来自内容的指示,您也会知道何时到达终点。 (或者你可以禁用按钮以防止过去)