My app consists of this one big frame (cp2) that contains two smaller frames: the top one contains the buttons that allow the user to switch between the different screens of the app and the bottom one contains the screens themselves (also frames). I initialize all screens at the beginning and then switch between them using their lift method.
The thing is, if the user clicks on a button to go to a different screen, unsaved changes made at the current one will be lost. I want to show a warning with a confirmation message, but I only see two ways of triggering the warning and I can't seem to figure out how to implement either.
Option 1 would be to add the warning to the exibirTela method: if the user clicks on a button that calls this method, the method will compare the values on the screen with the database before lifting another frame. Problem is, the method knows which frame is about to be lifted (t), but it doesn't know which frame is on top right now (the screen the user is about to leave).
Option 2 would be to issue the warning on a separate method, triggered by the event of the frame losing its place on top. The problem with this option is that no such event seems to exist for me to bind a method to it.
Either one works for me. Unless, of course, there's a reason I shouldn't use them, in which case, feel free to let me know. And, of course, any other suggestions are welcome, too.
The code is below. Sorry for the names in Portuguese. I added a few comments in English: hope this helps.
from tkinter import *
from tkinter.messagebox import *
import sqlite3
class cp2(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
root.title(string="Controle de Produtos e Pedidos")
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
# Here I create two frames: one for the buttons, one for the screens
barraOpcoes = Frame(self, bd=10, bg="yellow")
barraOpcoes.grid(row=0, column=0, sticky=EW)
barraOpcoes.columnconfigure(2, weight=1)
areaPrincipal = Frame(self, bd=10)
areaPrincipal.grid(row=1, column=0, sticky=NSEW)
areaPrincipal.columnconfigure(0, weight=1)
# Here I create a set (telasAplicacao) to which I'll add all the screens (frames)
self.telasAplicacao = {}
for tela in (telaInicial,
telaCrudFabricante, telaCrudRevista,
telaInclusaoFabricante, telaInclusaoRevista,
telaAlteracaoFabricante, telaAlteracaoRevista):
novaTela = tela(areaPrincipal, self)
self.telasAplicacao[tela] = novaTela
novaTela.grid(row=0, column=0, sticky=NSEW)
# Here I add the buttons that switch between frames
btInicio = Button(barraOpcoes, text="Início", command=self.exibirTelaInicial)
btInicio.grid(row=0, column=0, sticky=W)
btFabricantes = Button(barraOpcoes, text="Fabricantes", command=self.exibirTelaCrudFabricante)
btFabricantes.grid(row=0, column=1, sticky=W)
btRevistas = Button(barraOpcoes, text="Revistas", command=self.exibirTelaCrudRevista)
btRevistas.grid(row=0, column=2, sticky=W)
btSair = Button(barraOpcoes, text="SAIR", command=lambda: sairCP2(self))
btSair.grid(row=0, column=3, sticky=E)
# Always start with telaInicial on top: this is the welcome screen
self.exibirTelaInicial()
def exibirTela(self, t):
minhaTela = self.telasAplicacao[t]
minhaTela.exibirDados(codigo=kwargs["codigo"])
minhaTela.lift()
def exibirTelaInicial(self):
self.exibirTela(telaInicial)
def exibirTelaCrudFabricante(self):
self.exibirTela(telaCrudFabricante)
def exibirTelaCrudRevista(self):
self.exibirTela(telaCrudRevista)
class telaAlteracaoFabricante(Frame):
def __init__(self, parent, controller):
# lots of widgets on this frame
# also, all the other methods of this class
# and so on to all the other frames in telasAplicacao...