单独进程中的 GUI 触发操作

GUI triggering actions in a separate process

我在使用 python-多处理和 Tkinter GUI 时遇到问题。 我有两个进程:

以下是我正在努力解决的问题:

  1. 我想通过单击 GUI 按钮触发操作(f.eg。在第二个进程中启动特定任务)。
  2. 然后,当用户单击该按钮并且第二个进程中的任务开始时,GUI 将 window 更改为一个新的(已经知道如何更改 windows)。在新版本中还有一堆按钮,但我希​​望它们在第二个过程中的任务完成之前不可点击(或者点击时什么也不会发生)。

最好的方法是什么?

编辑:在我开始使用多处理之前,我的按钮和触发函数的代码如下所示:

button48g = tk.Button(self, text='48g', height = 10, width = 20, font=("Courier", 20), bg="WHITE", command=lambda: controller.show_frame(GUI.PageOne48g))
button48g.bind('<Button-1>', GUI.commands.leftClick)

其中 leftClick 是一个函数,f.eg。

def leftClick(event):
    print('Left click!')

现在,leftClick 函数在另一个进程中,称为 'test'。

queueTestResults = multiprocessing.Queue()
test = multiprocessing.Process(name='test', target=testProcess, args=(queueTestResults,))
test.start()

gui = GUI.GuiApp()
gui.root.mainloop()

while queueTestResults.empty():
    pass

someResults = queueTestResults.get()
test.join()

在我看来,使用 2 个队列比使用 1 个队列更容易完成

q_read,q_write = multiprocessing.Queue(),multiprocessing.Queue()

def slow_function():
    time.sleep(5)
    return "Done Sleeping"

def other_process_mainloop(q_write,q_read):
    # this process must get an inverted order
    while 1: # run forever
       while q_read.empty():
           time.sleep(0.1) # waiting for a message
       message = q_read.get()
       if message == "command":
           q_write.put(slow_function())
       else:
           q_write.put("Unknown Command")

# start our worker bee
P = multiprocessing.Process(name="worker",target=other_process_mainloop,
                                    # note inverted order
                            kwargs={q_write:q_read,q_read:q_write)

现在制作一个辅助函数来发送命令并等待响应

def send_command(command_string,callback_fn):
    def check_for_response():
       if q_read.empty():
          return gui.after(200, check_for_response)
       callback_fn(q_read.get())

    q_write.put(command)
    gui.after(200,check_for_response)

# now you can just call this method when you want to send a command

def on_button_clicked(event):
    def on_result(result):
        print("Work is Done:",result)
        gui.lbl.configure(text="All Done!! {}".format(result))            

    gui.lbl.configure(text="Doing some work mate!")
    send_command("command",on_result)