暂停课程&在继续之前等待子类返回用户输入值

时间:2018-01-04 13:28:11

标签: python model-view-controller pyqt

如何暂停当前/主类并等待子类执行并返回用户输入值然后继续主类的执行?

示例 :(搜索人员) 用户单击“搜索人员”按钮,mainClassController实例化searchPersonContoller,返回用户的选择(用户双击QTableView中的一行)。然后mainClassController创建一个显示所选用户的新选项卡

在上面的示例中,如何让mainClassController等待searchPersonContoller返回用户选择的值?

尝试:mainClass.py:

    self.searchPerson = searchPersonContoller()
    self.searchPerson.show()
    self.userSelection = searchPerson.getUserSelection()
        if self.userSelection > 0:
              #continue mainClass's code here

subClass.py:

def getUserSelection(self):
        self.selectionModel = self.ui.tbl.selectionModel()                  
        self.rowList = self.selectionModel.selectedRows()                               
        self.i = self.model.index(self.rowList[0].row(), 0, QtCore.QModelIndex())   
        self.close()
        return self.i.data()  

问题是当主类调用它时,子类没有返回值,它应该等到用户做出选择。

1 个答案:

答案 0 :(得分:0)

正如Jon Clements在上面的评论中指出的那样,主要课堂应该有一个信号。在玩了一下之后,这就是我想出来的

mainClass.py:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
import time

## Prepare initial data
a = 20
d = 20
tm0 = 10
t_m = tm0 * np.pi/180

def phi(t):
    return np.pi/4 + (6 + 0.1*t) * t

def r(t):
    return d/2 * (1 - phi(t)/phi(t_m))

def Fp(x,y,t):
    return -1/np.sqrt((x + r(t)*np.cos(phi(t)))**2 + \
    (y + r(t) * np.sin(phi(t)))**2)                  \                                      
    -1/np.sqrt((x - r(t)*np.cos(phi(t)))**2 +        \
    (y - r(t)*np.sin(phi(t)))**2)

X = np.arange(-1*a, a)
Y = np.arange(-1*a, a)
x,y=np.meshgrid(X,Y)

## generate image data
imageList = []
for t in range(tm0):
    imageList.append(Fp(x,y,t*np.pi/180))

## Create animation and video from 2D images
fig = plt.figure(figsize=(10,10))
ims = []
for i in range(len(imageList)):
    im = plt.pcolormesh(imageList[i], animated = True)
    ims.append([im])

ani1 = animation.ArtistAnimation(fig, ims, blit=True, 
repeat_delay=2000)
plt.colorbar()
t1=time.time()
ani1.to_html5_video()
t2=time.time()
print("2D image to video took: ", t2 - t1)

## Create animation and video from 3D images
t1 = time.time()
fig = plt.figure()
ax = Axes3D(fig)
ims = []
for i in range(len(imageList)):
    im = ax.plot_surface(x,y,imageList[i], antialiased=False, animated=True)
    ims.append([im])

ani = animation.ArtistAnimation(fig, ims, blit=True, repeat_delay=2000)
## plt.show()
t2 = time.time()
print("3D animation creation took: ", t2 - t1)
ani.to_html5_video()
t3=time.time()
print("3D animation to video took:", t3 - t2)