Tkinter如何在多框架应用程序中重置窗口/框架?

时间:2018-09-17 20:27:24

标签: python tkinter

我正在尝试用Tkinter编写一个简单的猜谜游戏gui,我正为一件特别的事情而苦苦挣扎。我正在使用此线程的解决方案: Switch between two frames in tkinter

我想要实现的是能够随时使用显示在每个页面上的退出按钮来退出,以这样的方式进行:每次单击退出按钮时,框架都会重置。 例如,这是我的代码的一部分:

class Window(tk.Tk):

def __init__(self, master = None):
    tk.Tk.__init__(self, master)
    tk.Tk.wm_title(self, 'Guessing Game')

    tk.Tk.resizable(self, width=False, height=False)

    self.min = 0
    self.max = 10

    self.frameName = tk.StringVar()


    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 = {}
    frames = (menuPage, SettingsPage, userPlayPage, compPlayPage)

    for F, geometry in zip((frames),['300x300']*len(frames)):

        page_name = F.__name__

        frame = F(parent=container, controller=self)

        self.frames[page_name] = (frame, geometry)

        frame.grid(row = 0, column = 0, sticky = 'nsew')

    self.show_frame('menuPage')


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

    self.frameName.set(cont)

    lowerFrame = tk.Frame(self)
    lowerFrame.place(relx = 0.1, rely = 0.8, width = 250)

    text, func = ('Quit', exit) if cont == 'menuPage' else ('Back', lambda: self.show_frame('menuPage'))

    quitBtn = ttk.Button(lowerFrame, text = text, width = 25,
                        command = func)
    quitBtn.pack(side = tk.BOTTOM)

    frame.tkraise()

class menuPage(tk.Frame):

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

    centerFrame = tk.Frame(self)
    centerFrame.place(relx = 0.1, rely = 0.1, relwidth = 0.8)

    userPlay = ttk.Button(centerFrame, text = 'Play' , width = 25,
                            command = lambda: controller.show_frame('userPlayPage'))
    userPlay.pack(side=tk.TOP)

    compPlay = ttk.Button(centerFrame, text = 'Play with computer' , width = 25,
                            command = lambda: controller.show_frame('compPlayPage'))
    compPlay.pack(side=tk.TOP)

    settingsBtn = ttk.Button(centerFrame, text = 'Settings' , width = 25,
                            command = lambda: controller.show_frame('SettingsPage'))
    settingsBtn.pack(side=tk.TOP)

class userPlayPage(tk.Frame):

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

    textFrame = tk.Frame(self)
    textFrame.place(relx = 0.1, rely = 0.1, relwidth = 0.8, relheight = 0.2)

    self.T = tk.Text(textFrame, height = 20, width = 100)
    self.T.pack()

    btnFrame = tk.Frame(self)
    btnFrame.place(relx = 0.1, rely = 0.3, relwidth = 0.8)

    min, max = controller.min, controller.max


    self.e1 = tk.Entry(btnFrame, width = 17)
    self.e1.grid(row=0,column=1)

    self.btnInsert = tk.Button(btnFrame, width = 10, text = 'Insert', command = (lambda: self.Guess(self.e1.get(), controller, min, max)))
    self.btnInsert.grid(row=0)

    self.guess = tk.IntVar()


    text = ('Guessing Game!\n', 'Guess a number between {} and {}\n'.format(min, max))
    for t in text:
        self.T.insert(tk.END, t)
    self.number = tk.IntVar()
    self.number.set(random.randint(min, max))


def Guess(self, guess, controller, min, max):

    self.T.delete(3.0, tk.END)
    self.guess.set(guess)
    if  self.guess.get() < self.number.get():

        self.T.insert(tk.END, "Aim higher")
    elif int(self.guess.get()) > self.number.get():
        # self.T.delete(3.0, tk.END)
        self.T.insert(tk.END, "Aim lower")
    elif int(self.guess.get()) == self.number.get():

        self.T.insert(tk.END, "You\'ve guessed")
        self.after(2000, lambda: self.T.delete(3.0, tk.END))
        newguess = random.randint(min, max)
        self.after(2000, lambda: self.number.set(newguess))
        self.after(2000, lambda: controller.show_frame('menuPage'))

每次我单击菜单页面上的“播放”按钮时,我都会抬起菜单页面框架。然后,游戏开始,创建新的猜测,您可以玩猜游戏,直到猜对为止-并且可以正常工作。但是,当我在游戏中单击退出按钮时,框架切换回menuPage,然后,当我再次提高userPlayPage时-游戏在上一次尝试中被“暂停”,而我想拥有的是:每次退出按钮单击框架应自行重置。问题就在这里-是否有任何方法可以跟踪当前位于哪个帧的顶部或检查该帧是否发生更改,因此每次更改时,一切都会恢复为默认状态。感谢您的帮助。

0 个答案:

没有答案