Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Returning an object from function to the main window.



  • 
    import sys
    
    from pyngrok import conf,ngrok
    
    from PySide6.QtWidgets import (
      QApplication,
      QLabel,
      QHBoxLayout,
      QMainWindow,
      QWidget,
      QPushButton,
      QLineEdit,
      QVBoxLayout,
      QGridLayout
    )
    
    def open_tunnel(port, ip_address):
    
        connect_to = ""
        if len(ip_address) == 0:
            connect_to = port
        else:
            connect_to = ip_address + ":" + port
    
        ngrok_tunnel = ngrok.connect(connect_to)
    
        return ngrok_tunnel
    
    
    def close_tunnel():
    
        ngrok.disconnect()
    
        return
    
    
    def get_tunnel(ngrok_tunnel):
    
        tunnel_url = ngrok_tunnel.public_url
    
        return tunnel_url
    
    
    def set_token_default(authtoken):
    
        conf.get_default().auth_token = authtoken
    
        return
    
    class Window(QMainWindow):
    
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.setWindowTitle("Grok it: v0.1")
            self.resize(400,100)
            self.centralWidget = QWidget()
            self.setCentralWidget(self.centralWidget)
            self.layout = QGridLayout()
            self.centralWidget.setLayout(self.layout)
            self.setupUI()
            self.setTokenButton.clicked.connect(self.tokenButtonFunction)
            self.connectButton.clicked.connect(self.connectButtonFunction)
            self.disconnectButton.clicked.connect(self.disconnectButtonFunction)
    
    
        def tokenButtonFunction(self):
            set_token_default(self.authTokenField.text())
            return
    
        def connectButtonFunction(self):
            ngrok_tunnel = open_tunnel(self.portField.text(),self.ip_addressField.text())
            return ngrok_tunnel
    
        def disconnectButtonFunction(self):
            tunnelname = get_tunnel(self.ngrok_tunnel)
            close_tunnel(tunnelname)
            return
    
        def setupUI(self):
    
    
            layout = QGridLayout()
            self.portLabel = QLabel("Port")
            self.ip_addressLabel = QLabel("IP Address")
            self.authTokenLabel = QLabel("AuthToken")
            self.portField = QLineEdit()
            self.ip_addressField = QLineEdit()
            self.authTokenField = QLineEdit()
            self.connectButton = QPushButton("Connect")
            self.setTokenButton = QPushButton("Set Auth Token")
            self.disconnectButton = QPushButton("Disconnect")
    
            self.layout.addWidget(self.ip_addressLabel,0,0)
            self.layout.addWidget(self.ip_addressField,1,0)
            
            self.layout.addWidget(self.portLabel,0,1)
            self.layout.addWidget(self.portField,1,1)
    
            self.layout.addWidget(self.connectButton,2,0)
    
            self.layout.addWidget(self.disconnectButton,2,1)
                    
            self.layout.addWidget(self.authTokenLabel,0,3)
            self.layout.addWidget(self.authTokenField,1,3)
            self.layout.addWidget(self.setTokenButton,2,3)
            return
    
        
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = Window()
        win.show()
        sys.exit(app.exec_())
    
    

    I’m trying to make a GUI for ngrok by using the pyngrok module. Problem being I can get it to connect but I can’t seem to pass it correctly so that it calls the ngrok process and allows me to disconnect from it. Any ideas what I’m doing wrong?



  • @Krathyn Save as attribute:

    class Window(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.setWindowTitle("Grok it: v0.1")
            self.resize(400, 100)
            self.centralWidget = QWidget()
            self.setCentralWidget(self.centralWidget)
            self.layout = QGridLayout()
            self.centralWidget.setLayout(self.layout)
            self.setupUI()
            self.setTokenButton.clicked.connect(self.tokenButtonFunction)
            self.connectButton.clicked.connect(self.connectButtonFunction)
            self.disconnectButton.clicked.connect(self.disconnectButtonFunction)
    
            self._ngrok_tunnel = None
    
        @property
        def ngrok_tunnel(self):
            return self._ngrok_tunnel
    
        def tokenButtonFunction(self):
            set_token_default(self.authTokenField.text())
            return
    
        def connectButtonFunction(self):
            self._ngrok_tunnel = open_tunnel(
                self.portField.text(), self.ip_addressField.text()
            )
    
        def disconnectButtonFunction(self):
            if self.ngrok_tunnel is not None:
                tunnelname = get_tunnel(self.ngrok_tunnel)
                close_tunnel(tunnelname)
    

Log in to reply