tobby48 4 years ago
parent
commit
bcda74a253

+ 68
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/lineDraw3.py View File

@@ -0,0 +1,68 @@
1
+from PyQt5.QtWidgets import QMainWindow, QApplication, QDesktopWidget
2
+from PyQt5.QtCore import Qt
3
+from PyQt5.QtWidgets import *
4
+from PyQt5.QtGui import QPainter, QBrush, QPen
5
+import sys
6
+import threading
7
+import time
8
+
9
+class Main(QMainWindow):
10
+    
11
+    def __init__(self):
12
+        super().__init__()
13
+
14
+        self.setMouseTracking(True)
15
+
16
+        self.setWindowTitle('mouse')
17
+        self.resize(900, 600)
18
+        self.xylist=[]
19
+        self.clicked = False
20
+        
21
+        msg = "함수형 스레드 {}".format(1)
22
+        th = threading.Thread(target=self.worker, name="[스레드 이름 {}]".format(1), args=(msg,))
23
+        th.start()  # 생성한 스레드를 시작한다
24
+        
25
+        self.show()
26
+
27
+    def mousePressEvent(self, e):  # e ; QMouseEvent
28
+        print('BUTTON Press')
29
+        if e.buttons() & Qt.LeftButton:
30
+            self.xylist.append(list())
31
+            self.clicked = True
32
+
33
+    def mouseReleaseEvent(self, e):  # e ; QMouseEvent
34
+        print('BUTTON RELEASE')
35
+        self.clicked = False
36
+#         self.mouseButtonKind(e.buttons())
37
+        
38
+    def mouseMoveEvent(self, e):  # e ; QMouseEvent
39
+        print('(%d %d)' % (e.x(), e.y()))
40
+        if self.clicked:
41
+            self.xylist[-1].append((e.x(), e.y()))
42
+            self.repaint()  # self.update()와 결과 동일
43
+
44
+    def paintEvent(self, event):
45
+#                 q.begin(self)
46
+        if len(self.xylist)> 0:
47
+            q = QPainter(self)
48
+            for b in self.xylist:
49
+                for c in range(len(b)-1):
50
+                    q.drawLine(b[c][0], b[c][1], b[c+1][0], b[c+1][1])
51
+                    
52
+    def worker(self, msg):
53
+        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
54
+            s.connect(('127.0.0.1', 4000))
55
+            print("클라이언트 소켓정보 : ", s)
56
+            while True:
57
+                line = input(':')
58
+                s.send(line.encode())    # 서버가 빈 데이터를 받고 연결을 종료할 수 있도록
59
+                if not line: break         # 빈 데이터를 먼저 보낸 후 루프를 탈출
60
+                data = s.recv(1024)
61
+                print("서버로 부터 전달받은 문자열 : ", data.decode())
62
+                self.repaint()
63
+
64
+
65
+#                 q.end()
66
+App = QApplication(sys.argv)
67
+window = Main()
68
+sys.exit(App.exec())   

+ 77
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/lineDraw4.py View File

@@ -0,0 +1,77 @@
1
+from PyQt5.QtWidgets import QMainWindow, QApplication, QDesktopWidget
2
+from PyQt5.QtCore import Qt
3
+from PyQt5.QtWidgets import *
4
+from PyQt5.QtGui import QPainter, QBrush, QPen
5
+import sys
6
+import threading
7
+import time
8
+import socket
9
+
10
+class Main(QMainWindow):
11
+    
12
+    def __init__(self):
13
+        super().__init__()
14
+
15
+        self.setMouseTracking(True)
16
+
17
+        self.setWindowTitle('mouse')
18
+        self.resize(900, 600)
19
+        self.xylist=[]
20
+        self.clicked = False
21
+        
22
+        self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
23
+        self.client_socket.connect(('127.0.0.1', 4000))
24
+        
25
+        msg = "함수형 스레드 {}".format(1)
26
+        th = threading.Thread(target=self.worker, name="[스레드 이름 {}]".format(1), args=(msg,))
27
+        th.start()  # 생성한 스레드를 시작한다
28
+        
29
+        
30
+        self.show()
31
+
32
+    def mousePressEvent(self, e):  # e ; QMouseEvent
33
+        if e.buttons() & Qt.LeftButton:
34
+            xy = "%d_%d_%d" % (e.x(), e.y(), 0)
35
+            self.client_socket.send(xy.encode())
36
+
37
+    def mouseReleaseEvent(self, e):  # e ; QMouseEvent
38
+        xy = "%d_%d_%d" % (e.x(), e.y(), 1)
39
+        self.client_socket.send(xy.encode())
40
+        
41
+    def mouseMoveEvent(self, e):  # e ; QMouseEvent
42
+#         print('(%d %d)' % (e.x(), e.y()))
43
+        xy = "%d_%d_%d" % (e.x(), e.y(), 2)
44
+        self.client_socket.send(xy.encode())
45
+#         if self.clicked:
46
+#             self.xylist[-1].append((e.x(), e.y()))
47
+#             self.repaint()  # self.update()와 결과 동일
48
+
49
+    def paintEvent(self, event):
50
+#                 q.begin(self)
51
+        if len(self.xylist)> 0:
52
+            q = QPainter(self)
53
+            for b in self.xylist:
54
+                for c in range(len(b)-1):
55
+                    q.drawLine(b[c][0], b[c][1], b[c+1][0], b[c+1][1])
56
+                    
57
+    def worker(self, msg):
58
+        while True:
59
+            data = self.client_socket.recv(1024)
60
+#             print("서버로 부터 전달받은 문자열 : ", data.decode())
61
+            xy = data.decode().split("_")
62
+            div = int(xy[2])
63
+            if div is 0:
64
+                self.xylist.append(list())
65
+                self.clicked = True
66
+            elif div is 1:
67
+                self.clicked = False
68
+            else:
69
+                if self.clicked:
70
+                    self.xylist[-1].append((int(xy[0]), int(xy[1])))
71
+                    self.repaint()  # self.update()와 결과 동일
72
+
73
+
74
+#                 q.end()
75
+App = QApplication(sys.argv)
76
+window = Main()
77
+sys.exit(App.exec())   

+ 34
- 0
src/kr/co/swh/lecture/python/pyqt5/drawing/multithread_server.py View File

@@ -0,0 +1,34 @@
1
+import socket
2
+import threading
3
+import time
4
+
5
+connList = []
6
+def worker(conn, addr):
7
+    print("{} 실행 중 ".format(threading.currentThread().getName()))
8
+    
9
+    while True:
10
+        data = conn.recv(1024)  # 변수 msg 은 바이트형이며, 클라이언트로 부터 받은 메세지가 저장된다. recv함수의 매개변수는 바이트 크기
11
+        if not data: break
12
+        for c in connList:
13
+            c.send(data)      # 메세지를 연결된 클라이언트에게 전달
14
+    conn.close()
15
+    print("{} 종료".format(threading.currentThread().getName()))
16
+    
17
+def run_server(port=4000):
18
+    host = ''
19
+    # socket.AF_INET 은 IP4v 주소체계(socket.AF_INET6 은 IP6v), socket.SOCK_STREAM은 소켓의 타입
20
+    # socket.AF_INET와 socket.SOCK_STREAM은 디폴트 값이므로 socket.socket() 로 코드를 작성해도 된다.
21
+    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
22
+        s.bind((host, port))
23
+        s.listen()
24
+        
25
+        while True: 
26
+            conn, addr = s.accept()     # accept함수의 결과는 튜플형이며, conn은 서버 소켓에 대한 정보, addr은 클라이언트의 정보
27
+            print("서버 소켓정보 : ", conn)
28
+            print("연결된 클라이언트 정보 : ", addr)
29
+            connList.append(conn)
30
+            th = threading.Thread(target=worker, name="[스레드 이름 {}]".format(addr), args=(conn, addr))
31
+            th.start()  # 생성한 스레드를 시작한다
32
+
33
+if __name__ == '__main__':
34
+  run_server()