下面两段代码,第一段没有多进程,第二段有进程,为什么有多进程的,变量不能是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 = [1,2,3]
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 = [1,2,3]
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()
|