Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Returning an object from function to the main window.
Forum Updated to NodeBB v4.3 + New Features

Returning an object from function to the main window.

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 2 Posters 234 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Krathyn
    wrote on last edited by
    #1
    
    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?

    eyllanescE 1 Reply Last reply
    0
    • K Krathyn
      
      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?

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

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

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved