tkinter中使用多进程报错?
下面两段代码,第一段没有多进程,第二段有进程,为什么有多进程的,变量不能是self开头,如果tree前面是self,就会报错,请问为什么,麻烦知道的说一下,谢谢#第一段代码
import tkinter as tk
from tkinter import ttk
from multiprocessing import Queue
import multiprocessing
import time
class Test:
def __init__(self):
self.data =
root = tk.Tk()
self.tree = ttk.Treeview(root,show='headings')
self.tree["columns"] = ('一','二','三')
self.tree.column("一",width=25,anchor='center')
self.tree.column("二",width=25,anchor='center')
self.tree.column("三",width=25,anchor='center')
self.tree.heading("一",text="一")
self.tree.heading("二",text="二")
self.tree.heading("三",text="三")
self.tree.grid(row=0)
self.entry = tk.Entry(root)
self.entry.configure(justify='center')
self.entry.insert(0,3)
self.entry.grid(row=1)
theButton = tk.Button(root,height=1,text='测试',command=self.test)
theButton.grid(row=2)
def test(self):
self.tree.insert('','end',values=self.data)
print(self.entry.get())
if __name__ == '__main__':
go = Test()
#第二段代码
import tkinter as tk
from tkinter import ttk
import multiprocessing as mp
class Test:
def __init__(self):
global tree
global entry
self.data =
root = tk.Tk()
tree = ttk.Treeview(root,show='headings')
tree["columns"] = ('一','二','三')
tree.column("一",width=25,anchor='center')
tree.column("二",width=25,anchor='center')
tree.column("三",width=25,anchor='center')
tree.heading("一",text="一")
tree.heading("二",text="二")
tree.heading("三",text="三")
tree.grid(row=0)
entry = tk.Entry(root)
entry.configure(justify='center')
entry.insert(0,3)
entry.grid(row=1)
theButton = tk.Button(root,height=1,text='测试',command=self.test)
theButton.grid(row=2)
theButton2 = tk.Button(root,height=1,text='多进程',command=self.doit)
theButton2.grid(row=3)
def job(self,q):
res = 0
for i in range(1000000):
res += i + i**2 + i**3
q.put(res) # queue
def doit(self):
#print(self.v.get())
q = mp.Queue()
p1 = mp.Process(target=self.job, args=(q,))
p2 = mp.Process(target=self.job, args=(q,))
p1.start()
p2.start()
p1.join()
p2.join()
res1 = q.get()
res2 = q.get()
print('multicore:',res1 + res2)
def test(self):
tree.insert('','end',values=self.data)
print(entry.get())
if __name__ == '__main__':
go = Test()
https://www.cnblogs.com/leijiangtao/p/11881009.html @逃兵 有空的话,麻烦帮解答一下,谢谢 第一段代码, 感觉在最后少了一行
tk.mainloop()
要不然tk图形界面无法显示出来 python初学者021 发表于 2021-3-31 10:25
第一段代码, 感觉在最后少了一行
tk.mainloop()
可以显示出来 第二段代码也有问题
应该是
p1 = mp.Process(args=(q,), target=self.job)
p2 = mp.Process(args=(q,), target=self.job)
注意是:
args=(q,)
有一个=号 针对第二段,我自己完善的代码
#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from multiprocessing import Queue
import multiprocessing as mp
import time
class Test:
def __init__(self):
global tree
global entry
self.data =
root = tk.Tk()
self.tree = ttk.Treeview(root, show='headings')
self.tree["columns"] = ('one','two','three')
self.tree.column("one", width=50,anchor='center')
self.tree.column("two", width=50,anchor='center')
self.tree.column("three", width=50,anchor='center')
self.tree.heading("one",text="one")
self.tree.heading("two",text="two")
self.tree.heading("three",text="three")
self.tree.grid(row=0)
self.entry = tk.Entry(root)
self.entry.configure(justify='center')
self.entry.insert(0,3)
self.entry.grid(row=1)
theButton = tk.Button(root,height=1,text="test",command=self.test)
theButton.grid(row=2)
theButton2 = tk.Button(root,height=1,text="multiproc",command=self.doit)
theButton2.grid(row=3)
def job(self,q):
res = 0
for i in range(1000000):
res += i + i**2 + i**3
q.put(res)
def doit(self):
q = mp.Queue()
p1 = mp.Process(args=(q,), target=self.job)
p2 = mp.Process(args=(q,), target=self.job)
p1.start()
p2.start()
p1.join()
p2.join()
res1 = q.get()
res2 = q.get()
print('multicore:', res1 + res2)
def test(self):
self.tree.insert('','end',values=self.data)
print(self.entry.get())
if __name__ == '__main__':
go = Test()
tk.mainloop()
python初学者021 发表于 2021-3-31 10:48
针对第二段,我自己完善的代码
我主要是想知道,
为什么有多进程的代码,有的变量不能是self开头,如果tree前面是self,就会报错, 逃兵 发表于 2021-3-31 09:49
https://www.cnblogs.com/leijiangtao/p/11881009.html
谢谢,能说说你是怎么找到的吗?发这个帖子前,我因为搜索了很久,都查不到相应的答案,文档又太长,看不下去,也看不懂 逃兵 发表于 2021-3-31 09:49
https://www.cnblogs.com/leijiangtao/p/11881009.html
你好,方便的话,帮我看一下问题,谢谢
页:
[1]