鱼C论坛

 找回密码
 立即注册
查看: 2738|回复: 9

[已解决]tkinter中使用多进程报错?

[复制链接]
发表于 2021-3-31 09:49:07 | 显示全部楼层 |阅读模式
20鱼币
下面两段代码,第一段没有多进程,第二段有进程,为什么有多进程的,变量不能是self开头,如果tree前面是self,就会报错,请问为什么,麻烦知道的说一下,谢谢

  1. #第一段代码
  2. import tkinter as tk
  3. from tkinter import ttk
  4. from multiprocessing import Queue
  5. import multiprocessing
  6. import time


  7. class Test:
  8.     def __init__(self):
  9.         self.data = [1,2,3]
  10.         root = tk.Tk()
  11.    
  12.         self.tree = ttk.Treeview(root,show='headings')        
  13.         self.tree["columns"] = ('一','二','三')

  14.         self.tree.column("一",width=25,anchor='center')
  15.         self.tree.column("二",width=25,anchor='center')
  16.         self.tree.column("三",width=25,anchor='center')
  17.         
  18.         self.tree.heading("一",text="一")
  19.         self.tree.heading("二",text="二")
  20.         self.tree.heading("三",text="三")
  21.         self.tree.grid(row=0)
  22.         
  23.         self.entry = tk.Entry(root)
  24.         self.entry.configure(justify='center')
  25.         self.entry.insert(0,3)
  26.         self.entry.grid(row=1)

  27.         theButton = tk.Button(root,height=1,text='测试',command=self.test)
  28.         theButton.grid(row=2)


  29.     def test(self):
  30.         self.tree.insert('','end',values=self.data)
  31.         print(self.entry.get())
  32.         
  33.         


  34. if __name__ == '__main__':     
  35.     go = Test()
  36.         
复制代码

  1. #第二段代码
  2. import tkinter as tk
  3. from tkinter import ttk
  4. import multiprocessing as mp

  5. class Test:
  6.     def __init__(self):
  7.         global tree
  8.         global entry
  9.         self.data = [1,2,3]
  10.         root = tk.Tk()
  11.    
  12.         tree = ttk.Treeview(root,show='headings')        
  13.         tree["columns"] = ('一','二','三')

  14.         tree.column("一",width=25,anchor='center')
  15.         tree.column("二",width=25,anchor='center')
  16.         tree.column("三",width=25,anchor='center')
  17.         
  18.         tree.heading("一",text="一")
  19.         tree.heading("二",text="二")
  20.         tree.heading("三",text="三")
  21.         tree.grid(row=0)
  22.         
  23.         entry = tk.Entry(root)
  24.         entry.configure(justify='center')
  25.         entry.insert(0,3)
  26.         entry.grid(row=1)

  27.         theButton = tk.Button(root,height=1,text='测试',command=self.test)
  28.         theButton.grid(row=2)

  29.         theButton2 = tk.Button(root,height=1,text='多进程',command=self.doit)
  30.         theButton2.grid(row=3)

  31.     def job(self,q):
  32.         res = 0
  33.         for i in range(1000000):
  34.             res += i + i**2 + i**3
  35.         q.put(res) # queue

  36.     def doit(self):
  37.         #print(self.v.get())
  38.         q = mp.Queue()
  39.         p1 = mp.Process(target=self.job, args=(q,))
  40.         p2 = mp.Process(target=self.job, args=(q,))
  41.         p1.start()
  42.         p2.start()
  43.         p1.join()
  44.         p2.join()
  45.         res1 = q.get()
  46.         res2 = q.get()
  47.         print('multicore:',res1 + res2)


  48.     def test(self):
  49.         tree.insert('','end',values=self.data)
  50.         print(entry.get())
  51.         
  52.         


  53. if __name__ == '__main__':     
  54.     go = Test()
  55.         
复制代码

最佳答案

查看完整内容

https://www.cnblogs.com/leijiangtao/p/11881009.html

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
chenyiyun + 1 + 1 鱼C有你更精彩^_^

查看全部评分

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

使用道具 举报

发表于 2021-3-31 09:49:08 | 显示全部楼层    本楼为最佳答案   
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-31 09:50:15 | 显示全部楼层
@逃兵    有空的话,麻烦帮解答一下,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-31 10:25:27 | 显示全部楼层
第一段代码, 感觉在最后少了一行

        tk.mainloop()


要不然tk图形界面无法显示出来
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-31 10:30:48 | 显示全部楼层
python初学者021 发表于 2021-3-31 10:25
第一段代码, 感觉在最后少了一行

        tk.mainloop()

可以显示出来
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-31 10:46:52 | 显示全部楼层
第二段代码也有问题

应该是
                p1 = mp.Process(args=(q,), target=self.job)
                p2 = mp.Process(args=(q,), target=self.job)


注意是:
args=(q,)
有一个=号
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-31 10:48:43 | 显示全部楼层
针对第二段,我自己完善的代码

  1. #!/usr/bin/python3

  2. import tkinter as tk
  3. from tkinter import ttk
  4. from multiprocessing import Queue
  5. import multiprocessing as mp
  6. import time

  7. class Test:
  8.         def __init__(self):
  9.                 global tree
  10.                 global entry
  11.                 self.data = [1,2,3]
  12.                 root = tk.Tk()

  13.                 self.tree = ttk.Treeview(root, show='headings')
  14.                 self.tree["columns"] = ('one','two','three')

  15.                 self.tree.column("one", width=50,anchor='center')
  16.                 self.tree.column("two", width=50,anchor='center')
  17.                 self.tree.column("three", width=50,anchor='center')

  18.                 self.tree.heading("one",text="one")
  19.                 self.tree.heading("two",text="two")
  20.                 self.tree.heading("three",text="three")
  21.                 self.tree.grid(row=0)

  22.                 self.entry = tk.Entry(root)
  23.                 self.entry.configure(justify='center')
  24.                 self.entry.insert(0,3)
  25.                 self.entry.grid(row=1)

  26.                 theButton = tk.Button(root,height=1,text="test",command=self.test)
  27.                 theButton.grid(row=2)

  28.                 theButton2 = tk.Button(root,height=1,text="multiproc",command=self.doit)
  29.                 theButton2.grid(row=3)

  30.         def job(self,q):
  31.                 res = 0
  32.                 for i in range(1000000):
  33.                         res += i + i**2 + i**3
  34.                 q.put(res)

  35.         def doit(self):
  36.                 q = mp.Queue()
  37.                 p1 = mp.Process(args=(q,), target=self.job)
  38.                 p2 = mp.Process(args=(q,), target=self.job)

  39.                 p1.start()
  40.                 p2.start()
  41.                 p1.join()
  42.                 p2.join()
  43.                 res1 = q.get()
  44.                 res2 = q.get()
  45.                 print('multicore:', res1 + res2)

  46.         def test(self):
  47.                 self.tree.insert('','end',values=self.data)
  48.                 print(self.entry.get())
  49.                


  50. if __name__ == '__main__':
  51.         go = Test()
  52.         tk.mainloop()

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

使用道具 举报

 楼主| 发表于 2021-3-31 10:53:44 | 显示全部楼层
python初学者021 发表于 2021-3-31 10:48
针对第二段,我自己完善的代码

我主要是想知道,
为什么有多进程的代码,有的变量不能是self开头,如果tree前面是self,就会报错,
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-31 13:41:46 | 显示全部楼层
逃兵 发表于 2021-3-31 09:49
https://www.cnblogs.com/leijiangtao/p/11881009.html

谢谢,能说说你是怎么找到的吗?发这个帖子前,我因为搜索了很久,都查不到相应的答案,文档又太长,看不下去,也看不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-4-7 15:29:10 | 显示全部楼层
逃兵 发表于 2021-3-31 09:49
https://www.cnblogs.com/leijiangtao/p/11881009.html

你好,方便的话,帮我看一下问题,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-25 11:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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