Manage Gtk+ window swapping

Asked

Viewed 267 times

1

Hail to you guys, I’m trying to make a program with Gtk+ (python3) to save some information in a comic book but I also want to identify users by login, (Quick explanation on how the program works). The user executes the program (opens a screen), enters the information and presses the login button, in that the window closes, and the window where the data would be presented appears, until then without problems however, I want to make a button to "end session" and as soon as pressed, the login window should be presented again but I’m not sure how to do this because if I put "self.set_visible(True)" right after the second window opens, the login window returns to appear even without the data window being closed. ps: The program is pretty raw yet why I want to solve this part first for dps to continue.

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()

1 answer

2


Analyzing your code and making no major changes, I believe that one of the ways to switch between the screens is.

Filing cabinet 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()

Filing cabinet 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()
  • Dear I appreciate your help, I am dealing with Gui for a short time and so my knowledge is still very limited, but with your answer I can already have an idea of how to work with multiple windows. ps: In a change I made recently in the code I was able to do what I wanted but I believe it was not the most professional way to do it because I was just hiding the windows and it increased the memory consumption right ?

  • That’s right, if the window doesn’t need to be pre-loaded or filled in it is better to destroy it so it doesn’t keep allocating memory.

  • Renato, man I came across another doubt about this same project, will you give me a help kk

Browser other questions tagged

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