Screen Transition - Tkinter

Asked

Viewed 1,721 times

0

Let’s say there is a screen with three buttons (Tkinter), each calling another screen when clicked. How to switch between these screens without getting that aspect of destroyed screen and a new appearing? The idea is that it seems that the contents of one screen have been replaced by another and not that it closes the window and opens another.

1 answer

3


Just put your components in separate frames, then you can change them:

menu

tela 1

import functools
import tkinter as t

class Tela(t.Frame):
    def __init__(self, parent, nome):
        t.Frame.__init__(self, parent)
        self.nome = nome
        t.Label(self, text='Voce esta na ' + self.nome).pack()

class Menu(t.Frame):
    def __init__(self, parent, *subtelas):
        t.Frame.__init__(self, parent)
        self.current_frame = self
        for subtela in subtelas:
            t.Button(subtela, text='Voltar',
                command=functools.partial(self.muda_tela, self)).pack()
            t.Button(self, text=subtela.nome, 
                command=functools.partial(self.muda_tela, subtela)).pack()

    def muda_tela(self, qual):
        self.current_frame.pack_forget()
        qual.pack()
        self.current_frame = qual

if __name__ == '__main__':
    root = t.Tk()
    root.resizable(0, 0)
    t1 = Tela(root, 'Primeira tela')
    t2 = Tela(root, 'Segunda tela')
    t3 = Tela(root, 'Terceira tela')    

    m = Menu(root, t1, t2, t3)
    m.pack()

    root.mainloop()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.