Last active
December 1, 2020 16:49
-
-
Save shiracamus/7d4b462a98375052ef98f30ca0669486 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from multiprocessing import Process, Pipe | |
from tkinter import Tk, ttk, StringVar | |
def execute(): | |
""" | |
コマンド実行 | |
""" | |
print('コマンドボタン 押下') | |
args = [arg for arg in map(str.strip, cmd.get().split(',')) if arg] | |
cmd.set("") | |
if args: | |
if args[0] == '加算': | |
start_proc(add, args) | |
elif args[0] == '減算': | |
pass | |
print('コマンドボタン 押下処理終了') | |
def start_proc(func, args): | |
""" | |
プロセス起動 | |
""" | |
print('プロセス作成') | |
proc = Process(name=func.__name__, target=func, args=(pipe_snd, args)) | |
proc.daemon = True | |
proc.start() | |
def add(pipe, args): | |
""" | |
加算プロセス | |
""" | |
ans = int(args[1]) + int(args[2]) | |
for data in range(ans, ans + 5): | |
pipe.send(data) | |
print('データ送出', data) | |
time.sleep(1) | |
def receive(): | |
""" | |
受信処理 | |
""" | |
if pipe_rcv.poll(): | |
data = pipe_rcv.recv() | |
print('データ受信', data) | |
ans.set(data) | |
root.after(200, receive) | |
if __name__ == "__main__": | |
pipe_rcv, pipe_snd = Pipe() # 双方向Pipe生成 | |
root = Tk() | |
frame = ttk.Frame(root, padding=1, borderwidth=10, relief='solid') | |
frame.pack(fill='x') | |
ans = StringVar() | |
ans.set("ans=") | |
ans_lbl = ttk.Label(frame, textvariable=ans, relief='solid') | |
ans_lbl.pack(fill='x', pady=5) | |
cmd = StringVar() | |
cmd.set('加算,1,2') | |
cmd_ent = ttk.Entry(frame, textvariable=cmd) | |
cmd_ent.pack(fill='x', pady=5) | |
exe_btn = ttk.Button(frame, command=execute, text="実 行") | |
exe_btn.pack(fill='x', pady=5) | |
root.after(200, receive) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment