鱼C论坛

 找回密码
 立即注册
查看: 2891|回复: 3

[已解决]为什么tkinter变量导致多进程报错??

[复制链接]
发表于 2021-3-27 01:18:05 | 显示全部楼层 |阅读模式
20鱼币
运行多进程就报错,是tkinter变量导致的,请问要怎么修改,谢谢!!


  1. import multiprocessing as mp
  2. import tkinter as tk


  3. class Test():
  4.     def __init__(self):
  5.         root = tk.Tk()
  6.         frame = tk.Frame(root,height=400,width=400)
  7.         frame.grid(row=0)
  8.         self.v = tk.StringVar()
  9.         entry = tk.Entry(frame, textvariable=self.v,width=3)
  10.         entry.grid(row=0, column=0)
  11.         entry.insert(0,10)
  12.         theButton1 = tk.Button(frame,height=4,width=10,text='执行',command=self.doit)
  13.         theButton1.grid(row=1,column=0)
  14.         root.mainloop()   

  15.     def job(self,q):
  16.         res = 0
  17.         for i in range(1000000):
  18.             res += i + i**2 + i**3
  19.         q.put(res) # queue

  20.     def doit(self):
  21.         #print(self.v.get())
  22.         q = mp.Queue()
  23.         p1 = mp.Process(target=self.job, args=(q,))
  24.         p2 = mp.Process(target=self.job, args=(q,))
  25.         p1.start()
  26.         p2.start()
  27.         p1.join()
  28.         p2.join()
  29.         res1 = q.get()
  30.         res2 = q.get()
  31.         print('multicore:',res1 + res2)
  32.         
  33.       
  34. if __name__ == '__main__':
  35.     go = Test()
复制代码
最佳答案
2021-3-27 01:18:06
  1. import multiprocessing as mp
  2. import tkinter as tk


  3. class Test():
  4.     def __init__(self):
  5.         global v
  6.         root = tk.Tk()
  7.         frame = tk.Frame(root,height=400,width=400)
  8.         frame.grid(row=0)
  9.         v = tk.StringVar()
  10.         entry = tk.Entry(frame, textvariable=v,width=3)
  11.         entry.grid(row=0, column=0)
  12.         entry.insert(0,10)
  13.         theButton1 = tk.Button(frame,height=4,width=10,text='执行',command=self.doit)
  14.         theButton1.grid(row=1,column=0)
  15.         root.mainloop()
  16.         


  17.     def job(self,q):
  18.         res = 0
  19.         for i in range(1000000):
  20.             res += i + i**2 + i**3
  21.         q.put(res) # queue

  22.     def doit(self):
  23.         print(v.get())
  24.         q = mp.Queue()
  25.         p1 = mp.Process(target=self.job, args=(q,))
  26.         p2 = mp.Process(target=self.job, args=(q,))
  27.         p1.start()
  28.         p2.start()
  29.         p1.join()
  30.         p2.join()
  31.         res1 = q.get()
  32.         res2 = q.get()
  33.         print('multicore:',res1 + res2)
  34.         
  35.       
  36. if __name__ == '__main__':
  37.     go = Test()
复制代码

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-27 01:18:06 | 显示全部楼层    本楼为最佳答案   
  1. import multiprocessing as mp
  2. import tkinter as tk


  3. class Test():
  4.     def __init__(self):
  5.         global v
  6.         root = tk.Tk()
  7.         frame = tk.Frame(root,height=400,width=400)
  8.         frame.grid(row=0)
  9.         v = tk.StringVar()
  10.         entry = tk.Entry(frame, textvariable=v,width=3)
  11.         entry.grid(row=0, column=0)
  12.         entry.insert(0,10)
  13.         theButton1 = tk.Button(frame,height=4,width=10,text='执行',command=self.doit)
  14.         theButton1.grid(row=1,column=0)
  15.         root.mainloop()
  16.         


  17.     def job(self,q):
  18.         res = 0
  19.         for i in range(1000000):
  20.             res += i + i**2 + i**3
  21.         q.put(res) # queue

  22.     def doit(self):
  23.         print(v.get())
  24.         q = mp.Queue()
  25.         p1 = mp.Process(target=self.job, args=(q,))
  26.         p2 = mp.Process(target=self.job, args=(q,))
  27.         p1.start()
  28.         p2.start()
  29.         p1.join()
  30.         p2.join()
  31.         res1 = q.get()
  32.         res2 = q.get()
  33.         print('multicore:',res1 + res2)
  34.         
  35.       
  36. if __name__ == '__main__':
  37.     go = Test()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-27 11:45:44 | 显示全部楼层

谢谢!!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-31 10:21:35 | 显示全部楼层

你好,如果方便的话,帮我看一下另一个问题,谢谢
https://fishc.com.cn/thread-193347-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-25 17:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表