Manage window switching Gtk +

1

Save Galley, I'm trying to do a program with Gtk + (python3) to save some information in a DB but I also want to identify the users by login, (Quick explanation on how the program works). The user executes the program (opens a screen), type the information and press the login button, in that the window closes, and the window where the data would appear appears, until then without problems however, I want to make a "close session "and as soon as it is pressed, the login window should be displayed again, but I am not sure how to do that, because if I put" self.set_visible (True) "shortly after the second window is opened, the login window returns to appear even though the data window has been closed. ps: The program is pretty raw yet because I want to solve this part first to continue dps.

Login.py

import gi
from Index import Index
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk


class LoginWin(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Contas")
        self.set_size_request(400, 600)
        self.set_gravity(Gdk.Gravity.CENTER)
        self.set_resizable(False)

        # Grid
        grid = Gtk.Grid()
        self.add(grid)

        self.login_txt = Gtk.Entry()
        self.login_txt.set_placeholder_text("Login")

        self.senha_txt = Gtk.Entry()
        self.senha_txt.set_placeholder_text("Senha")

        self.login_btn = Gtk.Button(label="Login")
        self.login_btn.connect("clicked", self.login)
        self.set_focus(self.login_btn)

        grid.add(self.login_txt)
        grid.attach_next_to(self.senha_txt, self.login_txt, Gtk.PositionType.BOTTOM, 1, 2)
        grid.attach_next_to(self.login_btn, self.senha_txt, Gtk.PositionType.BOTTOM, 1, 2)

        # sqlite

    def login(self, widget):
        self.hide()
        oi = Index()


def main():
    win = LoginWin()
    win.connect("destroy", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == '__main__':
    main()

Index.py

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class Index(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Table")
        self.set_size_request(300, 200)
        self.set_visible(True)

        grid = Gtk.Grid()
        self.add(grid)

        self.button = Gtk.Button(label="Encerrar sessao")
        self.button.connect("clicked", self.close)

        grid.add(self.button)

        self.show_all()
        self.connect("destroy", Gtk.main_quit)

    def close(self, widget):
        self.destroy()
    
asked by anonymous 22.09.2018 / 06:05

1 answer

2

By analyzing your code and without making any major changes, I think one of the ways to switch between screens is.

File LoginWin.py :

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Python 3 GTK+ 3

Arquivo LoginWin.py
"""
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from Index import Index


class LoginWin(Gtk.Window):
    def __init__(self):
        super().__init__()
        # Título da janela.
        self.set_title('Tela de login')
        # Definindo o ícone da janela (caminho relativo ou absoluto).
        # self.set_default_icon_from_file('../../_static/favicon.png')
        # Tamanho da janela.
        self.set_default_size(1366 / 2, 768 / 2)
        # Posição em que a janela será iniciada.
        self.set_position(Gtk.WindowPosition.CENTER)
        # Definindo uma borda entre a janela principal e o container (Grid layout).
        self.set_border_width(10)

        # Grid
        grid = Gtk.Grid.new()
        grid.set_row_spacing(10)
        self.add(grid)

        self.login_txt = Gtk.Entry.new()
        self.login_txt.set_placeholder_text('Login')

        self.senha_txt = Gtk.Entry.new()
        self.senha_txt.set_placeholder_text('Senha')

        self.login_btn = Gtk.Button.new_with_label('Login')
        self.login_btn.connect('clicked', self.login)
        self.set_focus(self.login_btn)

        grid.add(self.login_txt)
        grid.attach_next_to(self.senha_txt, self.login_txt, Gtk.PositionType.BOTTOM, 1, 2)
        grid.attach_next_to(self.login_btn, self.senha_txt, Gtk.PositionType.BOTTOM, 1, 2)

        # sqlite

    def login(self, widget):
        # Escondendo a janela.
        # self.set_visible(False)
        self.hide()

        # Exibindo a outra janela e passando pra ela LoginWin(Gtk.Window).
        # Isso porque vamos precisar exibir essa janela de novo.
        Index(parent=self).show_all()


if __name__ == '__main__':
    win = LoginWin()
    win.connect('destroy', Gtk.main_quit)
    win.show_all()
    Gtk.main()

Archive Index.py :

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Python 3 GTK+ 3.

Arquivo Index.py.
"""
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk


class Index(Gtk.Window):
    def __init__(self, parent):
        """Construtor.

        :param parent: Está recebendo LoginWin(Gtk.Window).
        """
        super().__init__()
        self.set_title('Table')
        # self.set_default_icon_from_file('../../_static/favicon.png')
        self.set_default_size(1366 / 2, 768 / 2)
        self.set_position(Gtk.WindowPosition.CENTER)
        self.set_border_width(10)

        # Atribuindo parent (LoginWin(Gtk.Window) em uma variável.
        self.parent = parent

        # Widges.
        grid = Gtk.Grid.new()
        self.add(grid)

        self.button = Gtk.Button.new_with_label('Encerrar sessao')
        self.button.connect('clicked', self.encerrar_sessao)
        grid.add(self.button)

        self.button_fechar = Gtk.Button.new_with_label('Fechar aplicativo')
        self.button_fechar.connect('clicked', self.close)
        grid.add(self.button_fechar)

    def encerrar_sessao(self, widget):
        print('Encerrando a sessão')

        # Escondendo a janela.
        # self.set_visible(False)
        # self.hide()

        # Destruindo a janela.
        self.destroy()

        # Exibindo a tela de login (LoginWin(Gtk.Window)).
        # self.parent.set_visible(True)
        self.parent.show_all()

    def close(self, widget):
        print('Fechando aplicativo')

        # Encerrando o aplicativo.
        Gtk.main_quit()
    
23.09.2018 / 05:21