質問編集履歴

2

使用しているモジュール等のバージョンを追記

2023/01/25 11:38

投稿

TheBullchannel
TheBullchannel

スコア33

test CHANGED
File without changes
test CHANGED
@@ -43,5 +43,8 @@
43
43
 
44
44
 
45
45
  ```
46
+ ### 使用しているバージョン
47
+ python 3.10
46
48
 
47
-
49
+ tkinter 8.6
50
+ asyncio-3.4.3

1

tkinterのボタンを押し、Win2のconnect_wsを呼び出し、class Aのconnect_wsを呼び出すように変更しました。

2023/01/25 06:57

投稿

TheBullchannel
TheBullchannel

スコア33

test CHANGED
File without changes
test CHANGED
@@ -10,8 +10,22 @@
10
10
  よろしくお願いいたします。
11
11
 
12
12
  ```
13
+ class A:
14
+ def __init__(self,master):
15
+    ~~省略
16
+
17
+   async def connect_ws(self):
18
+ uri = 'ws://localhost:18080/xxxxxxx/websocket'
19
+ async with websockets.connect(uri, ping_timeout=None) as ws:
20
+ while not ws.closed:
21
+ response = await ws.recv()
22
+
23
+  
24
+
25
+
13
26
  class Win2(tk.Frame):
14
27
    def __init__(self,master):
28
+    self.A = A()
15
29
     ~~省略
16
30
 
17
31
    def create_widgets(self):
@@ -20,11 +34,13 @@
20
34
    ~~省略
21
35
 
22
36
    def connect_ws(self):
23
-     uri = 'ws://localhost:18080/xxxxxxx/websocket'
24
- async with websockets.connect(uri, ping_timeout=None) as ws:
25
- while not ws.closed:
26
- response = await ws.recv()
27
- board = json.loads(response)
37
+     loop = asyncio.get_event_loop()
38
+ loop.create_task(self.A.connect_ws())
39
+ try:
40
+ loop.run_forever()
41
+ except KeyboardInterrupt:
42
+ exit()
43
+
28
44
 
29
45
  ```
30
46