0

I'm developing a program, currently it has an icon in the bottom corner of windows that when pressed twice it returns the main program, so if I close the window, pressing the icon it returns to where I left off, but when I press the file of execution twice or more, it opens more than one instance (there is more than one icon in the bottom corner), I need that instead of executing a new file, it simply executes the same file, bringing the version that was running in the background to the front

the main code:

if __name__ == "__main__":
root = QApplication(sys.argv)
app = HomeView()
app.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowTitleHint | Qt.CustomizeWindowHint)

    app = HomeController(app, HomeModel())
    root.setQuitOnLastWindowClosed(False)
    icon = QIcon("interface/images/icon.ico")
    tray = QSystemTrayIcon()
    tray.setIcon(icon)
    tray.setVisible(True)
    tray.activated.connect(app.show)
    
    root.exec_()

the homecontroller:

class HomeController(Controller):
def __init__(self, view, model, msg=None) -\> None:
super().__init__(view, model, msg=None)
self.chamados_controller = None
self.abrir_chamado_controller = None

        if not self.verificar_tokens():
            self._controllerToken.show()
        else:
            self.start()
        self._view = view
        self._model = model
        self.tray_icon = QSystemTrayIcon(QIcon("icon.ico"), self._view)
        self.tray_icon.setToolTip("HUB")
        self.tray_icon.activated.connect(self.tray_icon_activated)
        self.tray_icon.show()
    
    def tray_icon_activated(self, reason):
        if reason == QSystemTrayIcon.DoubleClick:
            if self._view.isHidden() or self._view.isMinimized():
                self._view.show()
        self._view.raise_()
        self._view.activateWindow()
    
    
    def closeEvent(self, event):
        event.ignore()
        self.hide()
        self.tray_icon.showMessage("Hub", "Rodando em segundo plano", QIcon.Information, 2000)
    
    def start(self):
        self.showMaximized()
        self.iniciar_sessao()
        self.definir_user()
        self.definir_maquina()
    
        self.chamados_controller = ChamadosController(
            ChamadosView(),
            ChamadoModel(self._model.api),
            self._view.lb_msg,
        )
    
        self.abrir_chamado_controller = AbrirChamadoController(
            AbrirChamadoView(),
            AbrirChamadoModel(self._model.api),
            self._view.lb_msg,
            self,
        )
    
        self._telas = {
            1: self.chamados_controller,
            2: self.abrir_chamado_controller,
        }
    
        self.definir_telas()
    
        self._view.btn_chamados.clicked.connect(
            lambda: self.navegar(1, "Chamados")
        )
        self._view.btn_abrirChamado.clicked.connect(
            lambda: self.navegar(2, "Abrir Chamado")
        )
        
        self.go_to_default_screen()
         # Conectando os sinais dos botões às funções correspondentes
        self._view.btn_minimize.clicked.connect(self.minimize_window)
        self._view.btn_close.clicked.connect(self.close_window)
        self._view.exit.clicked.connect(self.close_window)
        self._view.btn_maximize_restore.clicked.connect(self.maximize_restore_window)
        self._view.btn_toggle_menu.clicked.connect(self.menu)

I tried to run a local server, when the program is started it checks if the port is running with the application, if so, it sends a signal to the port, the port receives it and returns the last screen, but it is returning a screen that does not respond ( but when I click on the icon it works), I know that the fault must be at some point to pass to the thread but I don't know where it is or how to solve it the code:

def __init__(self, view, model, msg=None) -\> None:
super().__init__(view, model, msg=None)
self.chamados_controller = None
self.abrir_chamado_controller = None

        if not self.verificar_tokens():
            self._controllerToken.show()
        else:
            self.start()
        self._view = view
        self._model = model
        self.tray_icon = QSystemTrayIcon(QIcon("icon.ico"), self._view)
        self.tray_icon.setToolTip("Client Help Desk")
        self.tray_icon.activated.connect(self.tray_icon_activated)
        self.tray_icon.show()
        server_thread = threading.Thread(target=self.start_server)
        server_thread.start()
    
    
    
    
    def start_server(self):
        host = 'localhost'
        port = 37548
    
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    
        server_socket.bind((host, port))
    
        server_socket.listen(5)
    
        print(f"Servidor escutando em {host}:{port}")
    
        while True:
            client_socket, client_address = server_socket.accept()
    
            print(f"Conexão aceita de {client_address[0]}:{client_address[1]}")
           
            if self._view.isHidden() or self._view.isMinimized():
                self._view.show()
            self._view.raise_()
            self._view.activateWindow()
    
    
            client_socket.close()`
  • Please review your code and ensure that it's properly displayed in the post preview (see the [markdown help](//stackoverflow.com/editing-help) and the [code formatting](//meta.stackoverflow.com/a/251362) tips) before submitting modifications, as right now your code has various syntax issues that prevent us to properly understand it. – musicamante Aug 05 '23 at 17:28
  • Don't run code that modifies the GUI in another thread. Use a signal to notify that you want to show the GUI and in the associated slot show the GUI – eyllanesc Aug 05 '23 at 18:39
  • @eyllanesc thank you very much, it took me a while to understand, but it worked here :D – srliath Aug 05 '23 at 20:38

0 Answers0