質問するログイン新規登録

質問編集履歴

4

解決したコードの記載

2017/12/22 16:23

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -71,4 +71,72 @@
71
71
  もう片方が接続すればすぐに動くようになり、メッセージ交換も成功しますが、
72
72
  中止して閉じたい時や、windowをちょっと動かしたい時などに不便だなあと感じているのですが、どう解決したらいいのか悩んでいます。
73
73
 
74
- 解決策の提案、ご教授を頂けたら嬉しいです。
74
+ 解決策の提案、ご教授を頂けたら嬉しいです。
75
+
76
+
77
+ 動作停止が解決したコード
78
+ ```python
79
+ #メッセージ交換に使用する関数
80
+ def server_bind(port):
81
+ context = zmq.Context()
82
+ server = context.socket(zmq.REP)
83
+ server.bind("tcp://127.0.0.1:" + str(port))
84
+ return server
85
+
86
+ def client_connect(port):
87
+ context = zmq.Context()
88
+ client = context.socket(zmq.REQ)
89
+ client.connect("tcp://127.0.0.1:" + str(port))
90
+ return client
91
+
92
+ def server_recv(server, y, text_ctrl):
93
+ recv = server.recv_pyobj()
94
+ send = server.send_pyobj(y)
95
+ text_ctrl.SetValue(recv)
96
+
97
+ def client_recv(client, y, text_ctrl):
98
+ send = client.send_pyobj(y)
99
+ recv = client.recv_pyobj()
100
+ text_ctrl.SetValue(recv)
101
+
102
+ def button_event(event, CS_box, port_ctrl, text_ctrl):
103
+ port = port_ctrl.GetValue()
104
+ CS_select = CS_box.GetSelection()
105
+
106
+ if CS_select == 0:
107
+ server = server_bind(port)
108
+ s_recv = threading.Thread(target=server_recv,
109
+ args=(server,
110
+ "client君やあ(´・ω・`)",
111
+ text_ctrl))
112
+ s_recv.start()
113
+ else:
114
+ client = client_connect(port)
115
+ c_recv = threading.Thread(target=client_recv,
116
+ args=(client,
117
+ "server君やあ(´・ω・`)",
118
+ text_ctrl))
119
+ c_recv.start()
120
+
121
+ if __name__ == "__main__":
122
+ app = wx.App()
123
+
124
+ frame = wx.Frame(None, wx.ID_ANY, "test")
125
+ panel = wx.Panel(frame, wx.ID_ANY)
126
+
127
+ CS_box = wx.RadioBox(panel, wx.ID_ANY, pos=(0, 0),
128
+ choices=["server", "client"])
129
+
130
+ port_ctrl = wx.TextCtrl(panel, wx.ID_ANY, size=(40, 21), pos=(0, 50))
131
+ port_ctrl.SetMaxLength(4)
132
+ port_ctrl.SetValue("1234")
133
+
134
+ text_ctrl = wx.TextCtrl(panel, wx.ID_ANY, pos=(0, 120))
135
+
136
+ button = wx.Button(panel, wx.ID_ANY, "接続", pos=(0, 85))
137
+ event = lambda event:button_event(event, CS_box, port_ctrl, text_ctrl)
138
+ button.Bind(wx.EVT_BUTTON, event)
139
+
140
+ frame.Show()
141
+ app.MainLoop()
142
+ ```

3

変な代入があったので修正

2017/12/22 16:23

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -46,8 +46,8 @@
46
46
  frame = wx.Frame(None, wx.ID_ANY, "test")
47
47
  panel = wx.Panel(frame, wx.ID_ANY)
48
48
 
49
- CS_box = wx.radio_box = wx.RadioBox(panel, wx.ID_ANY, pos=(0, 0),
49
+ CS_box = wx.RadioBox(panel, wx.ID_ANY, pos=(0, 0),
50
- choices=["server", "client"])
50
+ choices=["server", "client"])
51
51
 
52
52
  port_ctrl = wx.TextCtrl(panel, wx.ID_ANY, size=(40, 21), pos=(0, 50))
53
53
  port_ctrl.SetMaxLength(4)

2

button_event関数に無駄な記述があった為修正

2017/12/20 16:38

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -29,9 +29,9 @@
29
29
 
30
30
  def button_event(event, CS_box, port_ctrl):
31
31
  port = port_ctrl.GetValue()
32
- CS_select = CS_box.GetStringSelection()
32
+ CS_select = CS_box.GetSelection()
33
33
 
34
- if CS_box.GetSelection() == 0:
34
+ if CS_select == 0:
35
35
  #ここで動作が停止する。
36
36
  server = server_bind(port)
37
37
  print(server_recv(server, "client君こんばんは(´・ω・`)"))

1

誤字の訂正

2017/12/20 15:50

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -64,11 +64,11 @@
64
64
  問題の部分を絞って書いたつもりですが、長かったらすいません。
65
65
 
66
66
  button_event関数を実行すると#ここで動作が停止する。とコメントに書かれてある部分で、
67
- windowsの移動がままならなくなったり、閉じようとした時に動作が停止しましたと表示されます。
67
+ windowの移動がままならなくなったり、閉じようとした時に動作が停止しましたと表示されます。
68
68
  wxpythonとzmqが入っていればコピペで動きますのでどういう感じか見てもらえればわかるかと思います。
69
69
 
70
70
 
71
71
  もう片方が接続すればすぐに動くようになり、メッセージ交換も成功しますが、
72
- 中止して閉じたい時や、windowsをちょっと動かしたい時などに不便だなあと感じているのですが、どう解決したらいいのか悩んでいます。
72
+ 中止して閉じたい時や、windowをちょっと動かしたい時などに不便だなあと感じているのですが、どう解決したらいいのか悩んでいます。
73
73
 
74
74
  解決策の提案、ご教授を頂けたら嬉しいです。