简体   繁体   中英

Multiple window program pyqt5 and python

I'm new here but I was looking for some answers and it is impossible for me to solve my problem, so I hope you can help me.

I am creating a python program to interact with an ABB robot. The program is composed with multiple files, each file contains a window and send or receive data to/from the robot.

To comunicate with the robot I create a socket connection, the program is the server and the robot is the client. The first window of the program creates the socket, the problem comes when the second window tries to send information to the robot because the clientsocket name/address is on the other file.

I tried to make global variables but is not working. I also tried to write the variable I want in a file and then read it, this transfer the variable but I cannot send to the robot because it is type str and I need socket Object.

Any idea how to transfer a client object from one file to another?

Any suggestions are welcome.

Thank you

This can be done by making the main window the parent of any secondary windows and making them open from the main window with the socket variable passed to the secondary window when it is created. For example:

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.socket = socket_information  # Set the socket connection up here

    def open_window_two(self):
        dialog = WindowTwoClass(self.socket)
        dialog.exec_()

Here the WindowTwoClass is the class of the secondary window from the the other file. This secondary window can't be a QMainWindow subclass, so depending on the functionality you want it will be a QWidget or QDialog subclass.

class WindowTwoClass(QWidget):
    def __init__(self, socket):
        QWidget.__init__(self)
        self.socket = socket

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM