Note that there are some explanatory texts on larger screens.

plurals
  1. POConfirmation box on file transfer over sockets using PyQT
    text
    copied!<p>I have a confirmation box which gets called when sending a file over the network. However, the function for the <code>Signal</code> is in the <code>MainWindow</code> class and uses variables from the <code>Worker</code> thread. But it doesn't work because the variables in the worker thread are not in the right scope.</p> <p>Any idea how I can pass the <code>msg</code> variable to the <code>saveFile</code> function in the <code>MainWindow</code> class from the signal?</p> <p><strong>Edit:</strong> What I needed to do was pass an argument to my signal but I figured it out. Sorry for the confusion. I was unsure what needed to be done.</p> <p>Here's a working example:</p> <pre><code>import socket import select import sys, os from PyQt4.QtCore import * from PyQt4.QtGui import * CMD_FILE = 1 CLIENT_PORT = 9001 CLIENT_HOST = '127.0.0.1' class MainWindow(QWidget): def __init__(self, parent=None): #super(QWidget, self).__init__(parent) super(MainWindow, self).__init__(parent) self.resize(100, 50) self.thread = Worker() self.thread.start() self.file_button = QPushButton('Send a File') self.connect(self.file_button, SIGNAL("released()"), self.sendFile) self.connect_button = QPushButton('Connect To Server') self.connect(self.connect_button, SIGNAL("released()"), self.connectToServer) self.connect(self.thread, SIGNAL('triggered(PyQt_PyObject)'), self.saveFile) self.layout = QFormLayout() self.layout.addRow(self.file_button, self.connect_button) self.setLayout(self.layout) def connectToServer(self): global s try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((str(CLIENT_HOST).strip(), CLIENT_PORT)) print "Connected to ", str(CLIENT_HOST) except: print "unable to connect" def clientAll(self, cmd, msg): #check if connnected send a message s.sendall(cmd + msg) def sendFile(self): filename=QFileDialog.getOpenFileName(self, 'Open File', '.') f = open(filename, 'rb') myFile = f.read() self.clientAll(chr(CMD_FILE), myFile) f.close() print filename, "Sent\n" def saveFile(self, msg): reply = QMessageBox.question(self, 'Message', "Are you sure you wish to download this file?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: filename = QFileDialog.getSaveFileName(self, 'Save File', '.') f = open(filename, 'wb') f.write(msg) f.close() print filename, "Recieved" else: pass class Worker(QThread): def __init__(self): QThread.__init__(self) self.exiting = False def __del__(self): self.exiting = True self.wait() def run(self): source_ip = '' #socket.gethostbyname(socket.gethostname()) PORT = 9001 ### Initialize socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind((source_ip, PORT)) # server_socket.listen(5) read_list = [server_socket] ### Start receive loop while True: readable, writable, errored = select.select(read_list, [], []) for s in readable: if s is server_socket: conn, addr = s.accept() read_list.append(conn) else: msg = conn.recv(12024) if msg: cmd, msg = ord(msg[0]),msg[1:] if cmd == CMD_FILE: if not msg: break self.emit(SIGNAL('triggered(PyQt_PyObject)'), msg) else: s.close() read_list.remove(s) if __name__ == '__main__': app = QApplication(sys.argv) win = MainWindow() win.setWindowTitle('CATS!!! &lt;3') win.show() sys.exit(app.exec_()) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload